A (physical) line with weight in iOS

I would like to implement a (physical) line with a weight in iOS that responds to the input of the accelerometer and colides against the viewing boundaries.

enter image description here

What is the easiest way to do this? I would prefer to avoid using external frameworks such as Box2D, unless, of course, my own solution is not too complicated.

+4
source share
1 answer

If you need it to accelerate under the influence of gravity and realistically jump when it hits the edge, you will need to use the physical structure (if you do not want to get a physics book and execute equations yourself), since nothing like that is built into UIKit.

I would recommend you try Chipmunk instead of Box2D . It was easier for me to use it with Cocoa, since it is pure C, not C ++. It also has an Objective-C wrapper, but the developer pays for it (the simple C library is free).

Here's a simple physics example of the iPhone Chipmunk I put together.

It uses an accelerometer and UIKit to draw - just replace the boxes with your own objects. (The accelerometer does not work in the simulator, you will have to try it on the phone).

UPDATE: now you have added an image that I understand, you mean bob on the line (I thought you mean UILabel that falls around the screen, lol!). Here 's another example that involves snapping between drawers. If you use this plus Documents for lumpy pieces , you can figure out what to do.

You need to attach one end of the string to a static object or to an infinite mass (not included in my example).

If you want a realistic string, you need to break it into several short restrictions instead of one long one, but I suggest you start with a simple one if you do not have much experience working with physical libraries.

+10
source

Source: https://habr.com/ru/post/1395177/


All Articles