[Solved] Need Physics Help Once Again
Posted: Sat May 16, 2009 8:37 pm
So here I am, once again, attempting to program something that somewhat resembles real-life physics. It's coming along fairly well, but I have a small issue that I need some help fixing.
First off a few implementation details (2D physics demo in SDL & OpenGL):
Creating an Object:
The object class has a vector of vertices and, depending on how many you've fed into it, draws it accordingly (whether it's a point, line, triangle, quad, or polygon).
It also has a vector of applied forces which once applied are removed from the queue. The AddForce function call takes a magnitude and a direction (angle in degrees: 0/360 is up, 90 is right, 180 is down, 270 is left). The problem occurs when you provide an angle that is not only on a single axis (ie. 240 is down and left).
When updated, the object iterates through each force in the queue and takes it apart into its x and y components, then adds them to totalForcex and totalForcey respectively. What should happen after this is where I begin to struggle.
Air resistance should always be opposite of the direction of motion (velocity) of an object. Right now I'm using the two if statements above (I removed second because it's same as the first but for the y-axis) to apply air resistance. This works fine when motion is on only one axis. However, when you are traveling in two directions (ie. down and left at 240 degrees) rather than traveling at an angle of 60 degrees right of the y-axis, it will travel at 45 degrees until the air resistance meets the vertical velocity, at which point it continues to travel directly left until the air resistance meets horizontal velocity. This is because both velocities are being updates at exactly the same rate even though they're different amounts.
My question to you is how would I go about finding and making the object travel at the correct slope (2 pixels over then 1 pixel down) for whatever the resultant angle of all of the acting forces is?
Sorry for the lengthy post, but without details it'd be rather difficult to guess what I'm trying to achieve.
First off a few implementation details (2D physics demo in SDL & OpenGL):
Creating an Object:
Code: Select all
Object *testObj = new Object( 0.80, 0.80 );
testObj->AddVertex( -0.10, 0.10, 255.0, 255.0, 0.0 ); //yellow
testObj->AddVertex( -0.10, -0.10, 255.0, 0.0, 0.0 ); //red
testObj->AddVertex( 0.10, -0.10, 0.0, 0.0, 255.0 ); //blue
testObj->AddVertex( 0.10, 0.10, 0.0, 255.0, 255.0 ); //cyan
testObj->AddForce( 2.0, 240 );
objManager.AddObject( testObj );
It also has a vector of applied forces which once applied are removed from the queue. The AddForce function call takes a magnitude and a direction (angle in degrees: 0/360 is up, 90 is right, 180 is down, 270 is left). The problem occurs when you provide an angle that is not only on a single axis (ie. 240 is down and left).
When updated, the object iterates through each force in the queue and takes it apart into its x and y components, then adds them to totalForcex and totalForcey respectively. What should happen after this is where I begin to struggle.
Code: Select all
//Calculate horizontal acceleration after air resistance
//NOTE: float air = 0.005
if( xVel - air > 0 ){
accelx = totalForcex - air;
}else if( xVel + air < 0 ){
accelx = totalForcex + air;
}else{
xVel = 0;
accelx = totalForcex;
}
//Calculate vertical acceleration after air resistance
//Same as above but for y axis
My question to you is how would I go about finding and making the object travel at the correct slope (2 pixels over then 1 pixel down) for whatever the resultant angle of all of the acting forces is?
Sorry for the lengthy post, but without details it'd be rather difficult to guess what I'm trying to achieve.
