Advertisement

weird sphere moving under gravity...

Started by October 13, 2002 03:37 PM
6 comments, last by maun 22 years, 4 months ago
Hi there! I''m doing a physical system but I''ve been faced to a weird problem recently. I still don''t know wether my math are going crazy or if it''s an opengl issue. I sumerized the code so that it''s easily readable : The problem is that the behaviour is a little bit different when I draw glutspheres or basic points. In fact, different means one is correct and the other isn''t! Thanks in advance for any help, suggestion...!
  
#include <GL/glut.h>


class mass {
public:
	float x,y,z;
	float vx,vy,vz;
	float m, inv_m;
	float Fx, Fy, Fz;

	mass () {};
	mass (float* pos, float* speed, float m);
	inline void resetForces() {Fx=Fy=Fz=0;}
	inline void addForceZ(float force) {Fz+=force;}
	void computePFD();
};


mass::mass(float* pos, float* speed, float m) {
	glPointSize(3.0f);
	this->x=pos[0];this->y=pos[1];this->z=pos[2];
	
	this->vx=speed[0];this->vy=speed[1];this->vz=speed[2];

	this->m=m;this->inv_m=1/m;
}


void mass::computePFD() {
	float dt=1.0f/60.0f;//60 Hz

	float ax=Fx*this->inv_m;float ay=Fy*this->inv_m;float az=Fz*this->inv_m;

	vx=dt*ax+vx;vy=dt*ay+vy;vz=dt*az+vz;

	x=dt*vx+x;y=dt*vy+y;z=dt*vz+z;

	if (z<=0) {
		z=-z;
		vz=-0.7*vz;
	}
}


mass M1;
mass M2;

void display(void) {


	//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	
	gluLookAt(1.20f,-4.50f,2.30f,0.0f,0.0f,0.0f,276.0f,1035.0f,66825.0f);

#if 1
	glTranslatef(M1.x,M1.y,M1.z);
	glutSolidSphere(0.01,8,4);

	glTranslatef(M2.x,M2.y,M2.z);
	glutSolidSphere(0.01,8,4);
#else
	glBegin(GL_POINTS);
		glVertex3f(M1.x,M1.y,M1.z);
		glVertex3f(M2.x,M2.y,M2.z);
	glEnd();
#endif
	glutSwapBuffers();
}


void idle(void) {

	M1.resetForces();
	M1.addForceZ(-9.8*M1.m);//gravity

	M1.computePFD();

	M2.resetForces();
	M2.addForceZ(-9.8*M2.m);//gravity

	M2.computePFD();

	glutPostRedisplay();

}


void reshapeWindowed(int w, int h) {
	glViewport(0,0,(GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0f,(float)w/(float)h,1.0f,20.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int main(int argc, char* argv[]) {
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

	glutInitWindowSize(640,480);
	glutCreateWindow("___ DEBUG ___");
	glutReshapeFunc(reshapeWindowed);


//Registration des fonctions

	glutDisplayFunc (display);
	glutIdleFunc(idle);

	float Pos1[3] = {-1.0f,-1.0f,2.0f};
	float Speed1[3]={1.0f,0.0f,-0.6f};
	M1=mass(Pos1,Speed1,1.0f);

	float Pos2[3] = {-1.0f,1.0f,1.0f};
	float Speed2[3]={1.0f,0.3f,0.6f};
	M2=mass(Pos2,Speed2,1.0f);


	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glutMainLoop();
	return 0;
}

  
Ok then, which one is correct and which one isn't? What exactly is correct or incorrect about it? Also, what does your computePFD() function do? I'm having a hard time reading the code.

I just finished writing my own trajectory system so I can help out with some of the physics stuff.

[edited by - Zipster on October 13, 2002 4:59:48 PM]
Advertisement
quote:
Original post by Zipster
Also, what does your computePFD() function do? I'm having a hard time reading the code.



First the function computes the acceleration from
the formula F = m*a -> a = F/m, where F/m is substituted by
inv_m which was initialized to equal 1/m (thus a = F*inv_m).

Since we're talkinbg 3d vectors here, the obove equation is applied to each vector component (ax,y,z*dt).

Second you have f(x) = v = t*a (base formula), but you need
the integral since you have discrete time steps (aka "frames")
, so the formul boils down to vnew=vold+a*dt.
Since we're still into 3D vectors, this again is done for each component (x,y,z).

Now we use the current velocity and assume it's constant over
the time fragment dt (which actually is a slight simplification)
and use it, to finally update the position vector given by
dx = vx*dt
dy = vy*dt
dz = vz*dt

and finally we have the force applied to our object.
The last if() statement is used to make the object bounce.

Hope that helps

[edit]
Qick add:
The points re acting correctly, while the upper sphere does
a weird jumping after the second or third bounce...
[/edit]

[edited by - darookie on October 13, 2002 5:02:12 PM]
If I understanded that code, the problem is in usage of glTranslatef, because when U call it for second time, U didn't "cancelled" someway the first call of it( the second sphere will be on position1+position2, not on position2), try to use something like this:

  glPushMatrix();glTranslatef(M1.x,M1.y,M1.z);glutSolidSphere(0.01,8,4);glPopMatrix();glPushMatrix();glTranslatef(M2.x,M2.y,M2.z);glutSolidSphere(0.01,8,4);glPopMatrix();  

which will cause that each call to glTranslate will be only local for each sphere... sorry for my bad english


Glubo The Mad

[edited by - glubo on October 13, 2002 5:06:23 PM]
Glubo the MadI am a signature virus. Please add me to your signature so that I may multiply.
Glubo: Yep

maun: I''ll try to explain that issue in more detail.
If you use points, you render objects without modifying the underlying transformation matrices.
When using glTranslate/glRotate etc. functions, you implicitly modify the transformation matrix.
So what your code really does is:

move Sphere1 to m1.x,m1.y,m1.z
render Shpere1
move Sphere2 to m1.x+m2.x,m1.y+m2.y,m1.z+m2.z
render Shpere2

glPushMatrix and glPopMatrix save and restore the current matrix using an internal stack.
You can use that method to do hierarchical transformation - check the Red Book for more info on that.

I know how to do physics, alright? I was just having a hard time seeing what the code was doing. Blame it on formatting, or lack of descriptive function name
Advertisement
quote:
Original post by Zipster
Blame it on formatting, or lack of descriptive function name


How about on a complete lack of appropriate documentation within the code!

I hate reading code that doesn''t have commenting in it! (HINT)

Timkin
first, thanks for your solutions, i''m quite new to opengl as it''s just for me a tool to design completly different stuff so I probably haven''t spent enough time in getting familiar with it..

to defend myself , I had removed all the unnecessary that may confuse people (that''s why there is a 1/60 for instance...). I tried to keep the code as short as possible and I assumed that the programming stuff was easy concepts that didn''t need further explanation! I do agree that my own code has comments. In addition, the name of functions are clear enough for me, but I''m French! so for instance PFD is probably a typical french abreviation

thanks again!

This topic is closed to new replies.

Advertisement