point based gravity

Game Editor comments and discussion.

point based gravity

Postby waggle » Wed Aug 02, 2006 4:10 am

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.
waggle
 
Posts: 14
Joined: Sat Jul 29, 2006 8:59 pm
Score: 0 Give a positive score

Postby Fuzzy » Wed Aug 02, 2006 6:32 am

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!)
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby DilloDude » Wed Aug 02, 2006 8:22 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby waggle » Fri Aug 04, 2006 5:50 am

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;
waggle
 
Posts: 14
Joined: Sat Jul 29, 2006 8:59 pm
Score: 0 Give a positive score

Postby Fuzzy » Fri Aug 04, 2006 9:21 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron