1

How can I move body ball in box2d like a volleyball without accelerating or dumping (with a constant speed).

Do I need a special formula for this?

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Sergozh
  • 11
  • 2

1 Answers1

1

In Box2D you move an object with forces. You can apply impulses or a linear force.

You can apply a impulse doing:

myBody->ApplyForce( force, myBody->GetWorldCenter() );

Or a force by doing:

myBody->ApplyForce(force, myBody->GetWorldCenter());

Note than a force is a b2Vec that you can construct doing:

b2Vec force = b2Vec2(0,50);

This force will only push the body up.

If you need a parabolic trajectory then you can create a force that has the component x and y greater than 0:

b2Vec force = b2Vec2(50,50);

Then the physics engine will do the rest.

You can also move to a specific position although I dont advice you to do that.

If you want more information about forces then follow this link.

Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • Yeah, I know that, but I need to implement parabola trajectory, using linear velocity, I need always constant speed of body ball, thats why I cant use impulses or forces. – Sergozh Dec 19 '12 at 17:37
  • OK `b2Vec force = b2Vec2(50,50);` might help, but I need to move ball after collision with player and 50, 50 vector is not what I want. – Sergozh Dec 19 '12 at 17:42
  • Yes this tutorial is good but they work only with x coord but I need both x & y – Sergozh Dec 19 '12 at 17:44
  • You can try the values by trial and error or you can try to understand how forces relate to the mass. But you want something like this? -> http://stackoverflow.com/questions/9924640/calculate-box2d-impulse-for-a-certain-angle-of-impact – Tiago Almeida Dec 19 '12 at 18:08
  • Do you mean that the body should move in a parabolic path, but it should move along that path at a constant speed? (which is quite difficult by the way...) – iforce2d Dec 19 '12 at 22:26
  • @iforce2d thats right, but not enough, I need to implement start speed after collision, like with bounce = 7, but after that my body moves very fast, and I can't catch him. Now I guess that I should use force to make start speed of a body, but I don't know an angle of my ball after collision with player. Thanks for reply. – Sergozh Dec 20 '12 at 13:38