Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to apply an impulse on a object with a particular force and at a particular angle in Box2d. If I am right the syntax would be the following:

body.GetBody().ApplyImpulse(new b2Vec2(direction, power),body.GetBody().GetWorldCenter());

The problem is my direction is in angles. I found a discussion where it was said that the way we can convert an angle into a vector would be as:

new b2Vec2(Math.cos(angle*Math.PI/180),Math.sin(angle*Math.PI/180));

Now I am not sure how to combine these two. In other words, if I wish to apply a force of 30 units at an angle of 30 degrees at the center of the object, how should I do it?

share|improve this question
up vote 3 down vote accepted

The first parameter of applyImpulse is a vector, the direction of the vector indicate the direction the impulse is to be applied and the length of the vector indicates the strength of the impulse.

your code;

new b2Vec2(Math.cos(30*Math.PI/180),Math.sin(30*Math.PI/180));

creates a vector at 30 degrees from the positive x-asis with a length of one. In order to make the vector longer, say 50 units in length(therefore 50 times stronger), just multiply both the x & y components of the vector by 50;

new b2Vec2(50*Math.cos(angle*Math.PI/180),50*Math.sin(angle*Math.PI/180));

similarity of you want a much weaker impulse, you can multiply by a fraction.

More readably, you could write;

 impulse = new b2Vec2(Math.cos(angle*Math.PI/180),Math.sin(angle*Math.PI/180));

finally you can use b2Rot() to calculate a vector from an angle;

impuse=new b2Rot(angle).GetXAxis(); // returns a vector at angle degrees to the x-axis
share|improve this answer

Your Answer


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.