Advertisement

Physics

Started by February 14, 2002 01:59 AM
11 comments, last by sandyshorts 23 years ago
''lo all, Having an issue with my math here and was wondering if I could grab a comment from anyone...I''m establishing some math code for object movement to be animated in opengl. Basically im letting the user to enter the x, y locations of the object and angle and velocity(initial vel) to toss an object. Then im gonna simulate using physics the objects decay over time from bouncing/collisions with the x-plane. snippet: float angle = 0; //user input - theta int v = 0; //user input - init velocity int x = 0; //user input - init x int y = 0; //user input - init y int time = 0; float xVel = 0; //calculated - velocity in x float yVel = 0; //calculated - velocity in y int accelX = 0; //acceleration in x, assumed no accel float accelY = -9.75; //gravity //get initial x and y velocity xVel = v*cos(angle); yVel = v*sin(angle); . . . . . . while (time < t) //where is any value for a constraint...ignore it. { //calculate change in velocity x //calculate change in velocity y //calculate new x position x = x + xVel*angle*time; //calculate new y position y = .5*accelY*time*time + yVel*time + y; if (y <= 0) y = abs(.9*(yVel*time + accelY*angle*time)); Calculations seem fine so far? I''m reviewing some physics and calc material online and trying to get the formulas created...
seems fine...

a few things though...

x = x + xVel*angle*time;
I guess this should be "sin(angle)" or something

y = .5*accelY*time*time + yVel*time + y;
why not: y = (.5*accelY*time + yVel)*time + y;


nik
Advertisement
replace this
x = x + xVel*angle*time;
with this, since there is no accelX
x = x + xVel*time;

and in this case,
y = .5*accelY*time*time + yVel*time + y;
if you are talking about projectile motion, then there is no initial yVel so it should be
y = .5*accelY*time*time + y;

oh, and g ( or accelY ) is 9.8 , not 9.75...not really abig deal, but that is the accepted value, at least according to my physics book and professor.

hope that is helpful... I just finished my first exam in Physics tonight, and it was about motion!

Lucas Henekswww.ionforge.com
Hmmm, the y value seems to keep growing...It should be getting to a point where it just flattens out. gotta love google for searching, still at it

output:

x: 0
y: 4
x: 0
y: 46
x: 1
y: 26
x: 3
y: 139
x: 6
y: 61
x: 10
y: 232
x: 15
y: 56
x: 20
y: 325
x: 26
y: 13
x: 33
y: 418
mmh this is going to cost me my physics rep.

but your taking the abs(y) somewhere i believe, so when y gets negative the abs make it positive again and it will keep increasing.

i say if(y<0) y =0;

and it depends also on your initial velocity, if you shoot really hard then your object isn''t coming down for a while.

11 km/s and your in space i believe
try using this more complete form for Y
y - yInitial =( vYinitial * sin theta )t - .5gt*t
and g should be -9.8.

and this one for X
x - xinitial = (vXinitial * cos theta)t

the equation for the path of the projecitle is

y = (x * tan theta) - ( (g*x*x) / (2(vInitial * cos theta)(vInitial * cos theta)))
maybe that will work (after translation to C++)
good luck

Edited by - lucinpub on February 14, 2002 3:48:18 AM
Lucas Henekswww.ionforge.com
Advertisement
Hello all...

I''ve only just browsed over all these posts together...
and I may be being REALLY picky....but strictly speaking...v in the original post is really just speed. If it were truely the velocity of the object it would be a "vector", as that is exactly what a velocity is...a vector quantity. As it stands, it is just a magnitude or "scalar" quantity.

The Real reason I''m saying this is that, the first thing to strike me was that using vectors may be a better method over all. A vector could be used for the position and another for the direction AND speed.

I know I''m offering no immediate help to the problem but hopefully people might think hey! that''s another way to do it...whether it be not as good or even better...

hope this is useful in some tiny way...
//get initial x and y velocity
xVel = v*cos(angle);
yVel = v*sin(angle);

This is already converting the scalar v into the vector (xVel,yVel)

and the position is (x,y)

What do you mean with working with vectors?

that you pass vectors in stead of calculating the individual x and y''s?
Basically yes I suppose...although I don''t offer this as the best way of doing things....only an alternative way.

I like to use a "vector" type something like:

typedef struct a_vector
{
float x;
float y;
float z;
}_vector;

then you could say things like (this is without having written anything)

_vector ObjPos;
_vector ObjVelocity;

...I just think that representing it like this can have a lot of advantages...just as a silly example you could find the angle between the direction the object is travelling in and the ground (or x plane)at any moment by doing a simple DotProduct with ObjVelocity and the axis in question....after normalising ObjVelocity of course.

It could also help with collisions etc...

Maybe I''ve got it wrong there...or maybe my approach is wrong, I suppose it all boils down to the same thing really, only my x, y and z are held in a struct...it just seems easier and cleaner to me to use a system like this rather than just having x''s and y''s lying about on there own.

AAhhh!!! just thought of a potentially good use for them!! (thank god for that I was getting lost in my own waffle....;op)

So, let''s say you have a ball bouncing along or just flying throug space for a bit. When it hits a surface, what do you do!!?? I don''t actually know off the top of my head but I expect it''ll be a little messy looking at the speed....and the ObjPos...and the direction it''s travelling blahrdy blah...

Using a nice vector method, you can just do a dot product (dot product is the king) to ind the angle between the velocity vector of the obj and the normal of the surface, and I bet it''s not too hard to do an intersection of a point or even the pos vector with the surface, to find when it hits....then all you need to do is work out the decay in speed etc, and you can do most of that without ever saying this x and that x...this y and that y etc.

I also think it''s nice just saying VecAdd(), VecSub() VecScale() etc. It''s cleaner, more readable and you can, sorry I can visualise whats going on a whole lot easier...

That''s it...I''m spent...please point out any obvious errors/flaws/crap/whadever in my dribble....;o)

Bye for now....

Now I must stop all this
oops! stopped short there! =0)

...stop all this blabber or sumthin....

This topic is closed to new replies.

Advertisement