Odd Physics Problem

Started by
3 comments, last by adam17 20 years ago
hey guys i have been working on a small, simple physics engine for my upcoming game. right now i am having a problem with my update code. i have 3 objects floating above my scene at the beginning. all 3 are supposed to drop (with updating gravity) at the same time. what happens is the first 2 initialized boxes start dropping. the first travels at the normal acceleration. the second box appears to moving at a tenth of the speed that the first is. (here is where it gets really weird) the third box does not start moving until the first two have collided and stopped on the floor. i have spent a long time checking my update function and cant find a thing. maybe someone can help me out. here is the code for the update function:

void Object::Update()
{
	static int Last_time = GetTickCount();
	int Current_time = GetTickCount();
	float GravDivTime = (Current_time - Last_time)/(1000*GRAVITY);
	bool vert_check = false;

	vel.y = vel.y - GravDivTime;
	pos.y = pos.y + vel.y;
	rot.x += 10 / GravDivTime;

	if((pos.y + vel.y) - radius <= GROUND)
	{
		pos.y = radius + GROUND;

		//Check for Vertex Collision

		int i=0;
		while(i<vertex.capacity() && vert_check!=false)
		{
			if(vertex[i] < -30 || vertex[i] > 30)
				vert_check = true;
			else if(vertex[i+1] < 0 || vertex[i+1] > 60)
				vert_check = true;
			else if(vertex[i+2] < -30 || vertex[i+2] > 30)
				vert_check = true;

			if(vert_check == true)
				vel.y = -(vel.y + DAMPER);

			i+=3;
		}
	}

	if((abs(vel.y) <= 0.001f) && (vel.y > 0))
	{
		active = false;
	}
	Last_time = GetTickCount();

	glPushMatrix();
		glTranslatef(pos.x, pos.y, pos.z);
		//glRotatef(rot.x, 1, 0, 0);

		//glRotatef(rot.y, 0, 1, 0);

		glColor4f(0, 1, 0, 1);
		glCallList(obj);
	glPopMatrix();
}
the update function is a public function of each object that reacts to the scene''s physics.
Advertisement
The static variable might be the cause. Try moving your timing code out of there into your main world update function, then passing the time difference as a parameter to your object update function.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
yes it will definitly be the static variable.

A static variable within a class method acts just like a global variable (just only accessable in that method)...
You will need to make Last_time a member variable of the 'Object' class instead.


the resond is because the value of Last_time is not unique to every instance of Object. So each object is sharing the same value, so the object appears to move at 1/10th the speed because thats the time between the first and second object having Update called, not the time between individual calls of Update to individual instances of Object.

| - My project website - | - email me - |

[edited by - RipTorn on April 22, 2004 8:48:33 PM]
WOW thanks alot guys! can anyone say DUH!!

all i did was declared Last_time as a global variable and put the Last_time = GetTickCount() in my Init function.

thanks again

quick question....
does anyone out there know of a place where i can find collision reaction dampening coefficeints. in english: i need to know how much to decrease the collision reaction after two objects collide. for example when a box falls on cement how much force should be applied upward to make it bounce?

[edited by - adam17 on April 23, 2004 1:17:24 PM]
what you can do is use the "constituion of restitution" (e) i think it is, we did it in mechanics. u have one of these set for a surface and the values range from 0 to 1.

e = speed of seperation of coliding objects
---------------------------------------
speed of approach of coliding objects


so to get the objects to leave at half the speed of the approach then you would set the e of that surface to a half
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!

This topic is closed to new replies.

Advertisement