1
\$\begingroup\$

I have a simple jBox2D (Java) implementation of a world with couple of Circle bodies that at the begining are static.

I want to make them movable when the user touches the circle. But I want them to be movable from the point where it's touched to its logical velocity direction.

How do I set the velocity / direction of the body after the touch so that it moves to the point that is logical to the touch?

10x

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Define "logical" a bit more clearly. Do you simply want to apply a force from where you click to the center of the circle? (That way, on an even plane, if you click just a bit to the right of the center of the circle, it will start rolling slowly, if you click on its edge it will roll faster)? \$\endgroup\$
    – TravisG
    Sep 13, 2011 at 10:21
  • \$\begingroup\$ yes I want to apply force when a point is touched and make it move to some path.. \$\endgroup\$
    – Blunt
    Sep 13, 2011 at 10:25
  • \$\begingroup\$ are we talking about easying? to move tour object to the clicked point using a smooth transition? \$\endgroup\$
    – FxIII
    Sep 13, 2011 at 11:22
  • \$\begingroup\$ no - let's look at this this way: how can I make a body move to a specified position on the screen? \$\endgroup\$
    – Blunt
    Sep 13, 2011 at 11:38

1 Answer 1

2
\$\begingroup\$

If you simply want it to move toward your finger and stop there, you need to call setAngularImpulse() using the vector between your finger location in world space (needs translation from screen space) and the world space location of the object itself. Once it gets near (see note) to your finger, begin slowing it down, or else just set it's velocity to zero.

NOTE: Due to floating point errors, it's unlikely to ever get exactly to the precise touch location, but it will get very, very close. So you need to determine if it's within a small radius of the touch location. Use pythagoras to determine the distance the touch location.


If you want kinematic motion, i.e. dragging the circles, and once you let go, they keep flying off in the direction of motion they were experiencing...

Look into kinematic bodies in Box2D. Kinematic basically means the position is controlled directly by some non-physics input, such as user input. This differs from dynamic bodies which can only be affected in a "natural" way by world physics. This is akin to saying that normal human beings (dynamic bodies) would bounce off a wall if they ran into it, whereas Superman (kinematic) would just bash that wall right down regardless that his weight and size couldn't possibly do that in reality. These sorts of objects tend to have infinite mass, as no matter what the user does, it must be followed as input. So if you had a bucket of bodies, and one of them was kinematic / controlled by dragging, then if you dragged it, all other bodies would fly apart to get out of the way of your drag-input.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .