# how to apply force/impulse perpendicular to the moving direction in box2d

How can i apply or impulse to an object perpendicular to its moving direction in a top down game. Like if object is moving up, i press left or right, then force or impulse should be applied to left or right direction(object can be at any angle).

• This sounds pretty straightforward. 1) get the current velocity (the Box2D API has methods to do this explained in the docs) 2) construct a vector perpendicular to this (existing Q&A here or high school linear algebra tutorials online cover this) 3) apply that perpendicular vector as a force or impulse (again, documented in the Box2D API). So it looks like you should already have everything you need to do this at your fingertips. What have you tried so far, and where specifically do you need our help? – DMGregory Dec 2 '19 at 19:10
• the way i was doing , player.ApplyForce( mult(new b2Vec2(Math.sin(player.getAngle()-Math.PI/2), Math.cos(player.getAngle()-Math.PI/2) ),force_mag), player.GetWorldCenter() ); – JustStarted Dec 2 '19 at 19:19
• Great. Does the result of that code differ from what you want? In what way? What specific problem do you need help fixing? – DMGregory Dec 2 '19 at 19:29
• with that code, it seems like force is applied at certain angles i.e when player is at 0, 90 angle. – JustStarted Dec 2 '19 at 19:32
• Isn't that exactly what you said you wanted? If I'm moving at a heading of 0°, a 90° force is perpendicular to the moving direction. What do you need to change/fix? – DMGregory Dec 2 '19 at 19:35

## 1 Answer

I used the below code which worked for me.

player.ApplyForce(  multiply(new b2Vec2(Math.cos(player.GetAngle()+Math.PI/2), Math.sin(player.GetAngle()+Math.PI/2) ),force_Mag),  player.GetWorldCenter() );