Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
1  
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)? –  TravisG Sep 13 '11 at 10:21
    
yes I want to apply force when a point is touched and make it move to some path.. –  Blunt Sep 13 '11 at 10:25
    
are we talking about easying? to move tour object to the clicked point using a smooth transition? –  FxIII Sep 13 '11 at 11:22
    
no - let's look at this this way: how can I make a body move to a specified position on the screen? –  Blunt Sep 13 '11 at 11:38

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.