Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Planet-like Gravity

Author
Message
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 2nd May 2009 16:48
For a while now i was wondering if i could do a small app in which i had a player walk on a planet, or better said around it, but I'm having some gravity issues. I don't know how exactly to set it up so it will change depending on the player position. Here's what I'm talking about:

I don't need any code snippets, just some explanations regarding what i should do.

P.s i can trick everything by just rotating the sphere, but that wouldn't be fun
prasoc
16
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 2nd May 2009 19:45
I would make an invisible sphere at the players position, point it at the centre of the big planet and move it down 9.81 (gravity) and get the player to follow it.
ABXG
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Canada
Posted: 2nd May 2009 20:02
You could probably create a variable called "loacalGravity" which you could alter depending on the location of the player. You would need to so some (not very complicated) math to determine the players location on the surface of your planet, measuring the angle of the player in terms of x, y, and z from the center of the planet should be sufficient for this.

As far as actually implementing gravity you could use something like Sparky's collision, you would simply have to increase the players velocity towards the center of the planet by localGravity. This would be harder as you would have to determine how to apply gravity to the players x, y, and z velocities in order to cause a constant acceleration towards the planets center.

------------------------------------
Currently 1300+ lines of code into an "over-the-shoulder" action RPG with combat based on rag-doll physics.
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 3rd May 2009 10:22
The trouble with prasoc's idea is the it only applies very close to the earth, so if you are making some sort of space sim, it looks silly.

I wrote something like this a while back, using newton's universal law of gravitation (which has now been shown not to always work, but is fine for your purposes).

http://en.wikipedia.org/wiki/Gravitational_acceleration
http://en.wikipedia.org/wiki/Gravitational_constant

Thus we need to work out the distance from the object to the earth. this is easy using pythagoras.
x = earthX-objX
y = earthY-objY
z = earthZ-objZ
dist = sqrt(x*x+y*y+z*z)
Hope thats right. long time since i did that in 3D.

now we know the distance, we can use the acceleration of the object towards the earth as something like this:

G = 6.67428 * 10^(-11)
acceleration = 0-(EarthMass*G)/dist

This assumes that mass is in Kg, distances are in metres and you are measuring in seconds, not perframe. to calculate per frame, simply divide this result by your frame rate.

Now to actually move the object you could use the method prasoc suggested, or use trig to find the unit vector, multiply this by the acceleration, and use each of these to move your object. good luck.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 3rd May 2009 21:26 Edited at: 3rd May 2009 21:30
Argh, still don't get it! This is what i have so far:

Sorry I'm a hard head right now, but i just can't figure it out
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 4th May 2009 10:21
OK, seems fairly good so far. What we need is a speed for the player. this can be
float speed = 0;
declared at the start.

and the acceleration applies every frame, not every second.
So:
acceleration/=dbFPS();

Not sure if that is the function. Haven't used GDK for ages.
Now every frame increase the speed by the acceleration
speed+=acceleration;

Now lets use Prasoc's idea. Create an object (dont make it invisible for now, need to see to debug), a box slightly small than your character. Lets say this has the id of 5.
You need to point this at the earth's position. This may be dbPointObject.
Now use
dbMoveObject(5, speed);
Which should move this box towards the earth by the players current speed.
The final step is to position the player at the same position as the box.
dbPositionObject(playervar,dbObjectPositionX(5),dbObjectPositionY(5),dbObjectPositionZ(5));
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 4th May 2009 15:00
Yes but wouldn't that make my object immovable? Because if i position it at the same X,Y and Z coordinates as object 5, i wouldn't be able to move my player object :\! Here's what i have so far
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 4th May 2009 16:53
Yeah, before you do any of that, make sure the box is at the place the player was at the end of the last turn so movement can be taken into account, and then point it at earth and make it fall.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 4th May 2009 17:56
Agh, can you get into more details? Like, at the end of the turn, i must reposition the box at the same position with the player?
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 4th May 2009 23:07
Yes.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 4th May 2009 23:17
Tried that, but it didn't work :\! There's still something I'm missing
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 5th May 2009 20:20
Seems roughly right. Several points. You are using the variable speed also to mean the speed your player moves, as in

suggest you rename the vars to playerspeed and gravspeed or some such. This playerspeed will need an arbitrary value.
Uncomment this:
acceleration/=dbScreenFPS();
change this:
dist = sqrt(px*px+py*py+pz*pz);
to just
dist = (px*px)+(py*py)+(pz*pz);
techincally it now is distance sqaured, but you need it square for forumla.
I would move this line:
dbPositionObject(5,dbObjectPositionX(playervar),dbObjectPositionY(playervar),dbObjectPositionZ(playervar));
To after you set the oldx etc since then you can use those values.
This:
dbYRotateObject(playervar,dbObjectAngleY(playervar)-1);
might be better as dbTurnObjectRight or whatever the function is.

can happen afetr the newx etc since they will be the same values, use newx etc in that function and find newx using object 5.

hope it helps.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 5th May 2009 21:39
Thanks jezza! Did some changes, but it partially works, because the it doesn't actually goes all the way around, and kinda get's stuck in the middle.
Later: agh.. dunno what i changed, now it just stays in place :\

I just can't understand what i am doing wrong. So, let's recap:
- I make two objects, a playercube and a "gravity"cube. I point the gravity cube towards the center of the sphere, and position the playercube at the gravity cube's position. I check for collision, and position the playercube on the surface of the "planet", and then, at the start of the new loop i position the gravity cube at the same position as the playercube :\
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 5th May 2009 21:53
why cant you add break points and debug it? You had probably better learn debugging now and get used to it.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 5th May 2009 22:30
Got it to work! Sorry for being such a pain in the "ass"! It seems that it worked thine before, but i had to pitch the object up and down in order to get it to move accordingly. Thank you allot jezza! You 2 prasoc. Here's a video of what i have until now. Time to perfect it, and turn it into a masterpiece ! Thanks again guys.
http://www.youtube.com/watch?v=gl2-OTi3Bbk
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 5th May 2009 23:17
now add jumping
and i hope you've uncommented the //acceleration/=dbScreenFPS();
line now, because otherwise gravity will be sixty times as strong as it should be
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 5th May 2009 23:31 Edited at: 5th May 2009 23:31
Well the reason i don't uncomment it is because something happens and i can't see anything! Just the blue background of the inter space...arg, window! : That's why i kept commenting it.
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 5th May 2009 23:39
hmmmmmmmm...
try just this:
acceleration = 0-((2000000000*G)/dist)/dbScreenFPS();
Oh and when the player hits earth, the speed needs to be reset to zero. That's very important. This makes jumping easy. On the first frame the jump key is pressed down, take an amount off speed, say 100 (experiment with this), so that it becomes minus ten. Thus it moves upwards until the gravity makes the speed more than zero, and it falls down again.
zapakitul
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: In my world
Posted: 5th May 2009 23:43
Already tried that version with /dbScreenFPS() before, but for some odd reason, it doesn't seem to work! :\
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 5th May 2009 23:45

If this makes no difference, debug the gravspeed variable after the divide and before, and see what changes it has made.

Login to post a reply

Server time is: 2024-11-05 18:48:10
Your offset time is: 2024-11-05 18:48:10