Jump to content

  • Log In with Google      Sign In   
  • Create Account

2D driving physics woes (problems) 2D Vector Math questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 19 October 2012 - 01:55 AM

Hi, I am working on a driving game at the moment, and I am having some serious problems with getting the physics to work right. Not just minor problems, I mean, I am having trouble keeping the numbers from ending up at -NaN within like 5 frames.
I have been following a tutorial that I found here on gamedev.net, but it is written in C#, and I am using C, so I am trying to translate everything into C. The tutorial I am following is here: http://www.gamedev.n...ysics-tutorial/
I did the first part and got what looks like a working rigid body simulator, I could apply a force vector and an offset vector and it moved in a way that looked sane. So I moved on to the rest of the tutorial, and I think I translated all the code pretty well, but then as soon as I ran my executable, everything went crazy.
I spent all of 3 days of free time (not that much with class and both of my jobs) looking for bugs. And I can't seem to find any. I found some discrepancies with the vector math in the tutorial and what I read, so I changed my functions to match the ones on the tutorial, and it almost worked, till I tried to use an angle other than 0.
Oh, I'm also using Linux and SDL2. It should compile easily on Windows if you have SDL2, my code is pretty portable.
Anyways, I'll attach the code, take a look at it and see if you can find any problems with my math. You should probably check my rigid body code, just in case I did something wrong but it just looked right to me.
Thank you in advance, I love you.

EDIT:: I posted updated code. The size is also smaller since I left out some unnecessary images. but the other file (a few posts down) has some code I left out before (accidentally) and includes a makefile so it should be enough to compile if you have SDL2.

Attached Files

  • Attached File  car.zip   557.91KB   44 downloads

Edited by prushik, 21 October 2012 - 03:19 AM.


Sponsor:

#2 Scorpie   Members   -  Reputation: 200

Like
0Likes
Like

Posted 19 October 2012 - 10:37 AM

Not sure if this fixes it but it looks like you are passing your angles in degrees and in physics.c you apply radians to degree instead you should be using degree to radians (angle / 180.0 * PI)

#3 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 19 October 2012 - 07:48 PM

Not sure if this fixes it but it looks like you are passing your angles in degrees and in physics.c you apply radians to degree instead you should be using degree to radians (angle / 180.0 * PI)

Thanks, I always mix those two up. Somewhere I put a comment in there about being confused as to which is should be. I found some type conversion problems as well, the degrees to radians problem might be the one final bug that I am looking for, I will let you know.

#4 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 21 October 2012 - 01:34 AM

Its true that my degree/radian conversions were wrong, however, seems that wasn't the only problem. Moving forward in a straight line seems to work ok now after some corrections, but turning still doesn't work correctly, and braking is even worse.
I added some code to draw lines representing force vectors so I could see what is happening and I am seeing some strange results.. Sometimes the front wheel force vectors point in the way I am trying to turn, sometimes they point straight ahead instead, and the back wheels tend to go in opposite directions when I try to turn. I can't understand why this happens.
I will attach my updated source to this reply, this time I will include my makefile and a source file that I forgot last time (sdl.c) which contains all the drawing functions.
Let me know if you see any more problems, I'm sure there are more...... Thank you for the help.

Attached File  car.zip   20.5KB   27 downloads

#5 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 22 October 2012 - 12:00 AM

Found another problem. I assumed that the tutorial used degrees and not radians, so I assumed that the value calculated by CalculateForce was in degrees, multiplying it by (180/PI) gives much better results, now moving straight looks fine, and turning works up to about 45 degrees, turning left can never exceed 90 degrees now and turning right past 45 degrees results in the car accellerating and rotating out of control.

#6 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 23 October 2012 - 06:49 PM

Ok. I got it all working pretty much. In the pointvel function, my tangent was not at all a tangent, it was pointing the wrong way. and more importantly, I had forgotten to convert back to relative coordinates in the calculate forces function. After fixing those, it looks much more realistic, and probably good enough for my game, since my game won't have really high speeds or racing or high speed turning (at least not too much). I just need to play with mass and friction and tire radius to get what I want now.
However, there are some things I am confused about. For one, in the tutorial, the author uses the dotproduct of the forward vector and the difference between the ground and the wheel velocity, but calls it the magnitude of the forward vector. Why do we not use the magnitude of the forward vector here? (I tried, it doesn't work well)

#7 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 23 October 2012 - 09:12 PM

Also, the vector projection implementation using in the tutorial (which I implemented in C) is different from all other sources I looked at (yet, it works).
I originally implemented vector projection based on what I read about vector projection, but I later changed it to match the tutorial.

Here is my original implementation:

void projectVEC(VEC2 *a, VEC2 *b, VEC2 *out)
{
	 double mag,tmp;

	 mag=magVEC(a);
	 tmp=dotVEC(b,a)/(mag*mag);


	 multiplyVEC(b,tmp,out);

	 return;
}


and the one I translated from the tutorial:
void projectVEC(VEC2 *a, VEC2 *b, VEC2 *out)
{
	 double tmp;

	 tmp=dotVEC(a,b);
	 multiplyVEC(b,tmp,out);


	 return;
}

Which one is correct?

#8 Inferiarum   Members   -  Reputation: 732

Like
0Likes
Like

Posted 27 October 2012 - 02:47 PM

I guess you want to project a onto b?
The second implementation works if b is normalized.
Your implementation is for the general case, however you have to divide by the squared norm of b, not a.

#9 bzroom   Members   -  Reputation: 646

Like
1Likes
Like

Posted 29 October 2012 - 06:50 AM

Silly author, Y u no explain things?

The reason the dot product is used instead of the vector magnitude, is because we are looking for the magnitude _in the forward direction_. Which means the result is signed. You could have a velocity _not_ in the forward direction, for which the dot product result would be negative. In other case, you may have some gigantic relative velocity, magnitude 1000, but if the vector is perpendicular to the forward direction, the dot product, and forward velocity magnitude, will be zero.

Sorry, this article is extremely dated. Good to see it's still got some legs though!

The correct projection is

Vec Project( Vec a, Vec onto )
{
Vec norm = onto.Normalized( );
return norm * Dot( a, norm ); //dot product against normalized "onto" vector.
}

You likely _dont_ want to use the magnitude of this vector, you likely just want the dot product result which is signed. (positive if vectors originally pointed in same direction, negative if not, zero if perpendicular)

#10 prushik   Members   -  Reputation: 164

Like
0Likes
Like

Posted 31 October 2012 - 04:50 AM

The reason the dot product is used instead of the vector magnitude, is because we are looking for the magnitude _in the forward direction_. Which means the result is signed. You could have a velocity _not_ in the forward direction, for which the dot product result would be negative. In other case, you may have some gigantic relative velocity, magnitude 1000, but if the vector is perpendicular to the forward direction, the dot product, and forward velocity magnitude, will be zero.

Sorry, this article is extremely dated. Good to see it's still got some legs though!


Great, thanks so much for the explanation!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS