0
\$\begingroup\$

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).

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Dec 2 '19 at 19:10
  • \$\begingroup\$ 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() ); \$\endgroup\$ – JustStarted Dec 2 '19 at 19:19
  • \$\begingroup\$ Great. Does the result of that code differ from what you want? In what way? What specific problem do you need help fixing? \$\endgroup\$ – DMGregory Dec 2 '19 at 19:29
  • \$\begingroup\$ with that code, it seems like force is applied at certain angles i.e when player is at 0, 90 angle. \$\endgroup\$ – JustStarted Dec 2 '19 at 19:32
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Dec 2 '19 at 19:35
0
\$\begingroup\$

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() );
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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