Jump to content
  • Advertisement
Sign in to follow this  
c0der_

OpenGL and physics

This topic is 2894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I have the following code, adding a normal force and a gravitational force to a 45 degree angled plane (for a 20unit mass).

I have used my calculator to verify I am getting the correct calculations and they are correct. However the acceleration vector below is not pointing in the direction of the 45 degree line. If I enter 90 degrees for the normal force instead of 45, the acceleration vector then points in the right direction at 45 degrees.

My understanding is the acceleration should point in the same direction as the net force. Why do I have to enter double the required angle to get the right result?

Please help !

glLoadIdentity();

// Reset The Current Modelview Matrix

Vector4 vGravity = Vector4(0, -9.8f*20.0f, 0);
Vector4 vNormal = Vector4(-9.8f*20.0f*sinf(_DEG_TO_RAD(45)),
9.8f*20.0f*cosf(_DEG_TO_RAD(45)), 0);
Vector4 vForce = vGravity + vNormal;
Vector4 vAcceleration = vForce/20.0f;

glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);

glVertex2f(0, 0);
glVertex2f(vAcceleration.x, vAcceleration.y);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex2f(0, 0);
glVertex2f(-10.0f*cosf(0), -10.0f*sinf(0));

glVertex2f(0, 0);
glVertex2f(-10.0f*cosf(_DEG_TO_RAD(45)), -10.0f*sinf(_DEG_TO_RAD(45)));

glVertex2f(0, 0);
glVertex2f(-10.0f*cosf(_PI_ON_2), -10.0f*sinf(_PI_ON_2));

glEnd();

Share this post


Link to post
Share on other sites
Advertisement
May be obvious question, but I'm assuming that _DEG_TO_RAD is something you created? Are you sure you didn't leave out a factor of two in the calculation?

Share this post


Link to post
Share on other sites
#define _DEG_TO_RAD(angle) ((angle) * _PI/180)

#define _PI 3.1415926535897932384626433832795f

I have also tried hardcoding the values and it gives the same results. No idea why.

I have even tried the calculation using my calculator. The results are identical but the behaviour is strange

Share this post


Link to post
Share on other sites
Oh I see.

Your force is comprised of two forces, not just the normal force.

If you input a normal force of 90 degrees (direction (1,0) for example), and add this to your gravity force (direction 0, -1), than your result is (1,-1), which is a vector at the 45 degree angle.

The same when you add a 45 degree angle to the gravity, you get a total force of (1,-2), which is not 45 degrees.

Share this post


Link to post
Share on other sites
indeed. I have input a normal force at a 45 degree inclined surface.

The values I get are:

vAcceleration.x = -6.9296465
vAcceleration.y = -2.8703537

atan(y/x) = 22.5 degrees

Am I calculating the normal force correctly?

Share this post


Link to post
Share on other sites
It's not very clear what're you're trying to do. If you want gravity to be the only influence and you want to simulate Newton's Laws, then the total normal force will be 0. The normal force into the plane will result in an equal and opposite force (Newton's Third Law of Motion) along the normal out of the plane. The force remaining will be a force tangential to the plane f[sub]t[/sub]= -m*g*sin(angle). If you want to resolve f[sub]t[/sub] into x & y components:

f[sub]x[/sub] = f[sub]t[/sub] * cos(angle)
f[sub]y[/sub] = f[sub]t[/sub] * sin(angle)

That's off the top of my head, so there's some sign manipulation required for the angles which will only change the direction of the X components. The Y component of the tangential force will be downwards.

EDIT: if you want to play with friction, the frictional force will be proportional to the normal force [ m*g*cos(angle), not the total normal force ] and in the direction oppositie to the velocity of the mass.

Share this post


Link to post
Share on other sites
I was trying to add forces individually by vector addition.

I have worked it out. The problem was:

Vector4 vNormal = Vector4(-9.8f*20.0f*sinf(_DEG_TO_RAD(45)), 9.8f*20.0f*cosf(_DEG_TO_RAD(45)), 0);

In fact it is supposed to be -9.8f*20.0f*cosf(_DEG_TO_RAD(45)), multiplied by the respective cosine and sines of the vector's components, so that the x component is -9.8*20.0f*cos(theta)*sin(theta) and the y component is -9.8*20.0f*cos(theta)^2. This ensures the magnitude of the normal force is mgcos(theta), which is what it's supposed to be, instead of just plain m*g.




It's not very clear what're you're trying to do. If you want gravity to be the only influence and you want to simulate Newton's Laws, then the total normal force will be 0. The normal force into the plane will result in an equal and opposite force (Newton's Third Law of Motion) along the normal out of the plane. The force remaining will be a force tangential to the plane f[sub]t[/sub]= -m*g*sin(angle). If you want to resolve f[sub]t[/sub] into x & y components:

f[sub]x[/sub] = f[sub]t[/sub] * cos(angle)
f[sub]y[/sub] = f[sub]t[/sub] * sin(angle)

That's off the top of my head, so there's some sign manipulation required for the angles which will only change the direction of the X components. The Y component of the tangential force will be downwards.

EDIT: if you want to play with friction, the frictional force will be proportional to the normal force [ m*g*cos(angle), not the total normal force ] and in the direction oppositie to the velocity of the mass.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!