PDA

View Full Version : Physics question



Wulf
08-14-2001, 11:03 AM
Ok, given position, speed, direction and rate of turning, how can I calculate the position of the object at some point in the future?

DFrey
08-14-2001, 11:18 AM
Is the rate of turning a constant (over a given time interval)? If not, it can get a little complicated.

[This message has been edited by DFrey (edited 08-14-2001).]

DFrey
08-14-2001, 12:12 PM
Also is this 2D or 3D motion? Also where is the center of the turn? If this is 3D motion, it would make things easier if you know what the turning axis is as well.

Wulf
08-14-2001, 05:27 PM
Yeah, I probably should of said that in the question. Speed & turning are constant. It's 2d motion: ignore the y-coordinate. Turning only occurs on the XZ plane.

DFrey
08-14-2001, 08:19 PM
Ok, then its relatively simple:
Here is some sample code that does that, you can pick out the important parts.



#include <math.h>
#include <iostream>

using namespace std;

const double pi=3.14159265;

int main(int num, char *cmd[])
{
double x0[2],x1[2],v[2],xp[2],w;
double r[2],n[2],radius,theta0,theta1;

// try out a sample condition

double t=1; // t: time interval in seconds

w=pi; //w: turning rate in radians per second
x0[0]=x0[1]=0; //x0: initial position
// v: inital velocity
v[0]=0;
v[1]=3*pi;

double s;
s=(w==0)?0:((w<0)?-1:1);
if(s==0)
{
// linear motion
x1[0]=x0[0]+v[0]*t;
x1[1]=x0[1]+v[1]*t;
}
else
{
// circular motion
double vmag=s*sqrt(v[0]*v[0]+v[1]*v[1]);
n[0]=-v[1]/vmag;
n[1]=v[0]/vmag;
radius=vmag/w;
r[0]=x0[0]+radius*n[0];
r[1]=x0[1]+radius*n[1];
xp[0]=x0[0]-r[0];
xp[1]=x0[1]-r[1];
theta0=atan2(xp[1],xp[0]);
theta1=theta0+w*t;
x1[0]=r[0]+radius*cos(theta1);
x1[1]=r[1]+radius*sin(theta1);
}

cout << "Final position " << x1[0] << " " << x1[1] << endl;


return 0;
}


Of course you also have to add code to handle the case when vmag=0.




[This message has been edited by DFrey (edited 08-15-2001).]

Wulf
08-15-2001, 06:35 AM
Thanks! I don't exactly understand all of it, but the results seem to make sense and movement looks smoother now

umount
08-16-2001, 03:46 AM
Hi !

Where can I find such code ??(book,pdf-file .... ??)

mfg dominik

DFrey
08-16-2001, 04:04 AM
I do not know offhand, I cooked that code up myself on the spur of the moment (my physics degree proved helpful for once http://www.opengl.org/discussion_boards/ubb/tongue.gif). It has a lot of room for improvement too.

But if you are looking for general physics routines, a search engine should point the way.

Michael Steinberg
08-16-2001, 03:23 PM
DFrey!
Could you think of a way in 3d incorporating roll and drift? It must have something to do with spirals, but my current physics teacher couldn't tell me how to do it. I ended up slicing it into small time periods of linear motion (actually per frame... http://www.opengl.org/discussion_boards/ubb/smile.gif), but I would think that this would lead to a slightly bigger radius than the realistic movement. Thanks!

grady
08-16-2001, 08:47 PM
Do you know any linear algebra? You ought to be able to use a forward vector, an up vector, velocities for each, and a rotation matrix for all of this. if you use the rotation matrix and you only have 1 angular velocity then i think it will cut down on the amount of computations the CPU has to do in each frame. i can help you with this if you don't know how to do it.

Michael Steinberg
08-17-2001, 05:56 AM
I'm actually able to do it in linear steps. Means, the camera rotates appropriately to the duration of the last frame, moves a step (one frame) and then again. That's no problem if you mean that. I'm actually talking about a spiral-like curve. Maybe I got you wrong. Any help is welcome!