Jump to content

Multiple gravity sources


xtreampb
 Share

Recommended Posts

Other than me doing constant gravity tests in my code, is there a way to set 2 objects to have a gravitational pull and which ever object i'm closer to, i will begin to 'fall' towards?

bool Life()
{
 while(death=false)
 {
   if(death==true)
   return death;
 }
}

 

I have found the secret to infinite life

 

Did I help you out? Like my post!

Link to comment
Share on other sites

Internally, the gravity value is just a force added to each active body. So if you have more complex logic like your describe, adding the force you want would be the only way.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

just so that i know how it is done calculated. I'm assuming that you are using the setforce function and applying it to the negative y axis. Is that value being increased every second? what is the equation you are using if you don't mind me asking?

bool Life()
{
 while(death=false)
 {
   if(death==true)
   return death;
 }
}

 

I have found the secret to infinite life

 

Did I help you out? Like my post!

Link to comment
Share on other sites

It just calls AddForce(gravity) where gravity by default is 0,-9.8,0 times mass:

Vec3 force = (spaceship->GetPosition(true) - planet->GetPosition(true)) * spaceship->GetMass();
spaceship->AddForce(force);

 

Technically, gravity increases as you get closer to an object though, so it should really be something like this:

Vec3 force = (spaceship->GetPosition(true) - planet->GetPosition(true)) * spaceship->GetMass();
const float gravityrange = 100;
force *= 1.0 - min(force.Length() / gravityrange,1.0);
spaceship->AddForce(force);

 

If you do this in the UpdatePhysics callback or script function, it will only get called for active bodies, and it will be called once per physics update.

 

Fun physics fact: See how I multiplied the force by the object's mass? That's the reason a piano and a walnut will fall at the same speed (not counting wind resistance).

  • Upvote 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I could be loosing my mind but was is the min function that you call, and what is 'the magnitude' of a vector.

bool Life()
{
 while(death=false)
 {
   if(death==true)
   return death;
 }
}

 

I have found the secret to infinite life

 

Did I help you out? Like my post!

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...