Page 1 of 1

point based gravity

PostPosted: Wed Aug 02, 2006 4:10 am
by waggle
By what method would you recommed creating gravity centred on a single point in the middle of a screen (i.e. rather than everything just being pulled down)?

The only way I can think of doing it would be to have a global array of velocity vectors (two for each object to be affected by the gravity) which would have to be updated each frame manually. Surely there's a less computationally intensive way of doing this.

PostPosted: Wed Aug 02, 2006 6:32 am
by Fuzzy
Games have been doing this for nearly 30 years; its not computationally expensive with the right technique. I'l let you think it over; reply if you find a way, or I will give you a solution tomorrow. (Sorry, I am on my way to bed!)

PostPosted: Wed Aug 02, 2006 8:22 am
by DilloDude
My method is to use the ReDir function (http://game-editor.com/forum/viewtopic.php?t=1934&highlight=redir) to change the angle, and make another function using the getangle function (included) which returns the size of the angle from A to B, which will adjust the velocity.

PostPosted: Fri Aug 04, 2006 5:50 am
by waggle
Thanks for the suggestions. Here's my code. I put this in the drawevent for the object. GravityWell is the gravity source. I did not include a mass for the object since as long as it is a tiny fraction of the mass of the "GravityWell" it is not significant to the calculations.

Yes, it's a mess but I was just trying to get some code working. :-)

Oh, and the createactor for the object had an initial xvelocity and yvelocity (otherwise the object would simply fall directly into the gravity well).


double d_Distance;
double d_GravityConst;

double d_Direction;
double d_deltaXcomponent;
double d_deltaYcomponent;
double d_Mass;
double d_TimeConst;
d_GravityConst = 10;
d_TimeConst = 10;
d_Mass = 10;


d_Distance = distance(xscreen, yscreen, GravityWell.xscreen, GravityWell.yscreen);
d_Direction= direction(GravityWell.xscreen, GravityWell.yscreen, xscreen, yscreen );
AngleNumber.textNumber = d_Direction;
// Convert Direction to radians from degrees
d_Direction = 2 * 3.14159265 * (d_Direction/360);

XVelNumber.textNumber=d_deltaXcomponent= -1*
(cos(d_Direction)*d_GravityConst*d_Mass*d_TimeConst) /( d_Distance*d_Distance);
YVelNumber.textNumber= d_deltaYcomponent=
(sin(d_Direction)*d_GravityConst*d_Mass*d_TimeConst) /( d_Distance*d_Distance);


xvelocity +=d_deltaXcomponent;
yvelocity +=d_deltaYcomponent;

PostPosted: Fri Aug 04, 2006 9:21 am
by Fuzzy
What you can do is have it run through say 10 or even 100 possible distances in global code, storing each result to a array, and when you do the draw, just take the distance and look it up in the array and get your falling speed.

gravity[distance]

or something of the like. This will save you a good deal of computation each draw, and should make your game run faster and smoother.