How to create the land (hills) like iOS game Contre Jour? (Using Box2d and OpenGL)
- I think we have array of bodies or fixture.
- When we to touch screen, determine touch location.
- If the touch location is not far from land, we begin to scan the array of bodies, and are looking for a body with coordinates closest to touch Location.
When case a touch Move, move the right body to a new coordinate (
What do you think, efficient to use a large number of bodies? And find for the right body by coordinates?
There is an array of vertices and triangles created by drawing the land (hills)? Is this true?
You can use the function b2World::QueryAABB to get a list of the fixtures in a given area, then check those for the best option. The Box2D testbed does this to find out which fixture to grab with the mouse so you could check out that source code. See also: http://www.iforce2d.net/b2dtut/world-querying
To move the body you can indeed use SetTransform, which would be good if the object does not need to interact with anything along the way. Another option might be to SetLinearVelocity to a velocity that will move the body to the dragged-to point in one time step. This is a better method if you want a continuous drag with the object being able to bump into things as it moves, because it does not teleport the body instantly to the finger position. If the body is a bullet body then it also prevents the user from dragging things through other objects, eg a static wall. Remember to set the velocity to zero when the finger is lifted :)