Jump to content

Gravity around object


Sakru
 Share

Recommended Posts

Hi guys! I having trouble with putting a gravity force inside a object. For example you have a planetoid, and you want to land on it when you have gravity set to 0 in space but have the gravity around the object? How could i achieve this? I tried to make a trigger on top of planetoid, but when i run i always run like on a platform ( i fly away ), and not on the round surface of the planetoid.

Link to comment
Share on other sites

You can do this, but you have to update this for every entity.

 

To determine the force that must be applied:

http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation#Modern_form

 

This equation would give you F (the magnitude of the gravity). You can probably just make up a G value though. The direction of F would be just towards the center of the planet. r is just the distance between the center of the planet and the object

 

Then you need to calculate the unit vector of F (I'll call it F_u):

http://www.vitutor.com/geometry/vectors/3d_vectors.html

 

Anyway, once you calculate F and the unit vector F_u, here is some pseudocode that should be close to the real implementation:

 

function Script:UpdatePhysics()

pos=self.entity:GetPosition(true) --position of object
pos_p=planet_entity:GetPosition(true) --position of planet

r=pos:DistanceToPoint(pos_p) --Calculate distance to the planet

F=G*(planet_mass*self.entity:GetMass())/(math.pow(r,2)) --Calculate the strength of the gravity

F_u_magnitude=math.abs(pos.x-pos_p.x)+math.abs(pos.y-pos_p.y)+math.abs(pos.z-pos_p.z)
F_u=Vec3(pos-pos_p)/F_u_magnitude

self.entity:AddForce(F_u.x*F, F_u.y*F, F_u.z*F) --This is the simulated gravity effect
...

end

 

 

I haven't tested this code, but this should in theory work.

 

A few notes:

  • Gravity should be disabled
  • The character controller will not work with this method since it's a custom physics object that relies on real-life gravity

 

Hope this helps!

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...