Thread: physics

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    physics

    i'm working on vehicle physics in OpenGL, and all seems well, except for one problem. the glTranslatef(); function dosn't seem to be working. at all. either that, or theres somthing wrong with the physics code itself which is very, very possible.

    vehicle.cpp (most of it. dosn't show includes and variable declarations):
    Code:
    void vphys::setup(float engine, float mass, float pos[2], float sdir[2])
    {
    	vd.epower = engine;
    	vd.m = mass;
    	//vd.p[2] = pos[2];
    	vd.dir[2] = sdir[2];
    }
    
    void vphys::mainroutine()
    {
    	//traction
    	vd.tractionF[0] = vd.dir[0]*vd.epower;
    	vd.tractionF[1] = vd.dir[1]*vd.epower;
    
    	//drag
    	vd.speed = sqrt(vd.v[0]*vd.v[0]+vd.v[1]*vd.v[1]);
    	vd.dragF[0] = -dragC*vd.v[0]*vd.speed;
    	vd.dragF[1] = -dragC*vd.v[1]*vd.speed;
    
    	//roll resistance
    	vd.rrF[0] = -rrC*vd.v[0];
    	vd.rrF[1] = -rrC*vd.v[1];
    
    	//total longtitudinal force
    	float longF[2];
    	longF[0] = vd.tractionF[0]+vd.dragF[0]+vd.rrF[0];
    	longF[1] = vd.tractionF[1]+vd.dragF[1]+vd.rrF[1];
    
    	//acceleration
    	vd.a[0] = longF[0]/vd.m;
    	vd.a[1] = longF[1]/vd.m;
    
    	//velocity
    	vd.v[0] = vd.v[0]+dt*vd.a[0];
    	vd.v[1] = vd.v[1]+dt*vd.a[1];
    
    	//final position
    	vd.p[0] = vd.p[0]+dt*vd.v[0];
    	vd.p[1] = vd.p[1]+dt*vd.v[1];
    	glTranslatef(0, 0, vd.p[1]);
    }
    from my render(); function:
    Code:
    	glScalef(0.01, 0.01, 0.01);
    	_peVphys->setup(900, 1000, startposition, startdirection);
    	_peVphys->mainroutine();
    	pModel->draw();
    I really have no idea whats wrong. Although I havn't coded in 3.5 weeks, so i'm a little rusty. (more than I was to begin with :P).

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Observations:

    You have the following code:
    Code:
    	//vd.p[2] = pos[2];
    	vd.dir[2] = sdir[2];
    You probably want a memcpy here instead of assignment. Even if you did want just an assignment, you are assigning element #2 out of a 2 element array. Remember, zero based. So you are walking off the end of that array, bad news.

    You may want to create your own Vector2D class. Then you can overload some useful math operators and have them behave like built in types. That would be much clearer to read.

    Also when you call translate you use the 3rd parameter which is for Z translation usually, are you sure you want that?

    Maybe more errors but I just glanced at it, I'll take another look later when I have more time. Good luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    >>You probably want a memcpy here instead of assignment. Even if you did want just an assignment, you are assigning element #2 out of a 2 element array. Remember, zero based. So you are walking off the end of that array, bad news.<<

    That crossed my mind, but I wasn't sure, so I left it like it was.

    >>You may want to create your own Vector2D class. Then you can overload some useful math operators and have them behave like built in types. That would be much clearer to read.<<
    That crossed my mind as well, but again, I wasn't sure, so stuck with arrays for the time being.

    >>Also when you call translate you use the 3rd parameter which is for Z translation usually, are you sure you want that?<<
    yup. Y is up in my engine, and you start looking down the negative Z.

    Anyway thanx! I'll get started on the fixes asap and i'll post if I have any other questions.

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    arg! still nothing. heres the revised code:

    vehicle.cpp
    Code:
    void vphys::setup(float engine, float mass, float pos[2], float sdir[2])
    {
    	vd.epower = engine;
    	vd.m = mass*gravity;
    	vd.p.x = pos[0]; vd.p.z = pos[1];
    	vd.dir.x = sdir[0]; vd.dir.z = sdir[1];
    }
    
    void vphys::mainroutine()
    {
    	//traction
    	vd.tractionF.x = vd.dir.x*vd.epower;
    	vd.tractionF.z = vd.dir.z*vd.epower;
    
    	//air-drag
    	vd.speed = sqrt(vd.v.x*vd.v.x + vd.v.z*vd.v.z);
    	vd.dragF.x = -dragC*vd.v.x*vd.speed;
    	vd.dragF.z = -dragC*vd.v.x*vd.speed;
    
    	//rolling resistance
    	vd.rrF.x = -rrC*vd.v.x;
    	vd.rrF.z = -rrC*vd.v.z;
    
    	//total longtitudinal force
    	vd.longF.x = vd.tractionF.x + vd.dragF.x + vd.rrF.x;
    	vd.longF.z = vd.tractionF.z + vd.dragF.z + vd.rrF.z;
    
    	//acceleration
    	vd.a.x = vd.longF.x/vd.m;
    	vd.a.z = vd.longF.z/vd.m;
    
    	//velocity
    	vd.v.x += dt*vd.a.x;
    	vd.v.z += dt*vd.a.z;
    
    	//position
    	vd.p.x += dt*vd.v.x;
    	vd.p.z += dt*vd.v.z;
    
    	glTranslatef(0, 0, vd.p.z);
    }
    IMO everything seems fine. i'm completely stumped. any ideas?

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    To be honest, I'd really have to see more of the program to see what is really going on. If you want to attach a zip of all the source I would be happy to look at it later tonight when I get home. If you don't feel comfortable with that, that's fine too. I don't see anything jumping out at me with the code you posted. What you might try, is commenting out the complicated stuff (drag, traction) and just get the bare bones positional updates working first. Step through in a debugger, make sure there aren't any zero valued variables throwing off your calculations. That's where I would start.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    i finally got it to work, but only by accident. I was trying to translate a vehicle scaled to 0.02 into a different position (not via the physics code), and that didn't work either. So, I tryed translating it by a larger number, and it moved. From that I found that I had to divide the about I wanted to move it by the scale value. (In that case, 0.5/0.02=25), so then i tryed multiplying my physics final position by a very large number, and it began to move, but slower than it should have. Then eventually I was led to a variable (dt), which I had set to 0.01. I changed it to 1.0 and it worked perfectly at normal speed.

    Thanx anyway though, and thanks again for the help before, as it was working even less before then .

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Physics Parser
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-20-2004, 06:08 PM
  2. what physics courses
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-22-2003, 12:19 PM
  3. AP Physics B
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 05-04-2003, 12:33 PM
  4. February 1, 2019...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 98
    Last Post: 08-03-2002, 07:24 AM
  5. Physics: Kind of like a cannonball...
    By Cheeze-It in forum Game Programming
    Replies: 11
    Last Post: 09-12-2001, 01:53 PM