0
\$\begingroup\$

I am trying to create a physics engine for my game, and need to cancel out any lateral velocity for when entities turn. I have tried to mimic this page's example, but it is not working. I have objects sliding sideways, as if they continued to have no friction.

Could you please check my friction equation below and make sure I have the proper variables plugged into the right places? If everything checks out I can do some more intensive debugging.

Vector2d lateralnormalized = new Vector2d(1, new Angle(heading));
lateralnormalized.invert();
double dot = Vector2d.dotProduct(lateralnormalized, velocityvector);
Vector2d lateralvelocity = new Vector2d(lateralnormalized);
lateralvelocity.multiplyInline(dot);

// Multiply by mass to get impulse
lateralvelocity.multiplyInline(-1 * getMass());
applyImpulse(new Vector3d(lateralvelocity));

Vector2d(int, Angle) creates a new vector of magnitude int at angle Angle.

Vector2d.invert() rotates the vector pi/2 radians (90 degrees).

\$\endgroup\$
7
  • \$\begingroup\$ Don't do anything with angles. In general, if you use angles in physics for anything other than displaying things to the user, you are going to have problems. What is wrong with Vector2d lateral = velocity; lateral.normalizeInline()? \$\endgroup\$ – user41442 Feb 6 '14 at 21:07
  • \$\begingroup\$ The heading is independent of the velocity. The entity could be pointing north, but moving north-east. \$\endgroup\$ – Yos233 Feb 6 '14 at 21:17
  • \$\begingroup\$ I meant velocityDesired, sorry. The heading is still a vector. Using angles for representing directions is ... problematic, at best. Also we aren't going to debug your code for you, as this wouldn't help anyone else. Is there something about the equations you don't understand? \$\endgroup\$ – user41442 Feb 6 '14 at 21:24
  • \$\begingroup\$ I do not have a desired velocity. I have the heading an object is pointing, and it's current velocity. I could not just rotate the velocityvector to match the heading, that would not be physically accurate. ---- While the heading could be a vector, I think it is easier to keep it as an angle, then convert it to a normalized vector on demand. If I do not use angles, how would you suggest I rotate the heading of an object 45 degrees? How would I calculate how much force to apply to each axis as a result of engine thrust? \$\endgroup\$ – Yos233 Feb 6 '14 at 21:37
  • \$\begingroup\$ I do not understand why the impulse I apply to my object does not cancel its lateral velocity. I'm looking for an overview of my code, to help me see if I misplaced a variable. If code help belongs on StackOverflow only, I'll gladly ask it there. \$\endgroup\$ – Yos233 Feb 6 '14 at 21:39

Your Answer

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

Browse other questions tagged or ask your own question.