Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Christopher Winstanley · Aug 09, 2010 at 04:54 PM · physicsgravity

How can I implement Planet Gravity in my game?

Hi Everyone,

I am implementing a space simulator where fireballs are colliding with planets. I want the fireballs to be effected by gravity of planets but don't want them to collide every time.

My Current code in the Update function is:

var fgX = 0; var fgZ = 0;

var distanceX = fireball.transform.position.x - planet.position.x;

var distanceZ = fireball.transform.position.z - planet.position.z;

var theta = Mathf.Atan2(distanceZ,distanceX); var rsq = distanceZ*distanceZ+distanceX*distanceX;

if (rsq < 400) { fireball.transform.position.x = fireball.transform.position.x-(Mathf.Cos(theta)); fireball.transform.position.z = fireball.transform.position.z-(Mathf.Sin(theta));

var fg = 0.5*planet.rigidbody.mass*fireball.rigidbody.mass/(rsq);

fgX += fg*Mathf.Cos(theta);

fgZ += fg*Mathf.Sin(theta);

         fireball.rigidbody.velocity.x += (fgX/fireball.rigidbody.mass);
         fireball.rigidbody.velocity.z += (fgZ/fireball.rigidbody.mass);

}

I got this from the following website example : http://www.wxs.ca/js/jsgravity/

At the moment the fireball is just flying into the planet every time as soon as it hits the 400 rsq distance. I want it to be dependent on how far away the fireball is when it enters this area.

Any help would be much appreciated even if you can't see where my code is going wrong another way to go about the gravity would be great.

Thanks

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ares · Aug 09, 2010 at 06:04 PM 0
Share

I see a number of things...and I've retyped this comment a couple of times already...so I'll just get to the point if you don't $$anonymous$$d. Line 9 should be sqrt((z*z)+(x*x)) Line 11 should be a larger number depending on the mass of the objects. Line 16 you're gravitation constant (0.5) seems too big...like hugely big. Between the earth and moon is like 0.000000000067 to something.
Something to keep in $$anonymous$$d. Planets are not blackholes. For the most part, if an asteriod/meteor/fireball isn't aimed at the planet, then it's going to miss the planet. theta looks funny but I'm not sure why.

avatar image Ares · Aug 09, 2010 at 06:13 PM 0
Share

Sorry, just thought of something else. You need to calculate the escape velocity of the planet. If the meteor is traveling faster than that speed, and it's not aimed at the planet...it will always miss (kenetic eneragy > potential energy). Personally, I say fake the effect. If the meteor is X close, change its direction by Y degrees.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Christopher Winstanley · Aug 09, 2010 at 07:05 PM

Thanks everyone for your comments, I have found another way to implement this

function FixedUpdate() { var cols : Collider[] = Physics.OverlapSphere(transform.position, gravityRange); var rbs : Array = new Array(); for (c=0;c

     if (cols[c].attachedRigidbody && cols[c].attachedRigidbody != rigidbody) {
         var breaking :boolean = false;
         for (r=0;r<rbs.length;r++) {
             if (cols[c].attachedRigidbody == rbs[r]) {
                 breaking=true;
                 break;
             }
         }
         if (breaking) continue;
         rbs.Add(cols[c].attachedRigidbody);
         var offset : Vector3 = (transform.position - cols[c].transform.position);
         var mag: float = offset.magnitude;
         cols[c].attachedRigidbody.AddForce(offset/mag/mag * rigidbody.mass);
     }
 }

}

This was on the unity wiki and works perfectly.

**Edit Smorpheus: The full source code (C# & JS) can be found here: http://www.unifycommunity.com/wiki/index.php?title=Gravity

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skovacs1 · Aug 09, 2010 at 10:29 PM 0
Share

If this solved the problem, please mark it as the accepted answer.

avatar image
0

Answer by Lee Falin · Aug 09, 2010 at 06:36 PM

I disagree with the issue mentioned in the comments about your gravitational constant, that is relative to the mass. If you're following the javascript example and using small masses, it should be fine.

To find the problem you first need to figure out if the problem is in your code or in your assumptions about the behavior of the system. How your fireballs behave is going to depend very much upon their initial location and size relative to the planet as well as their velocity.

If you notice in the JS simulation, there are several trajectories you can start a ball on that will always crash it into the planet. In fact, it's a lot harder to not crash the ball than it is to crash it.

I'm assuming that you have the rest of the function in your code somewhere that actually moves the fireballs. If so, you'll need to experiment with different starting positions and velocities and print out some debug information to see why the system isn't behaving as you expect.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Physics gravity appears very weak 2 Answers

How to make telephone wires 1 Answer

Faux Gravity Prolem? #2 2 Answers

Changing gravity on collision 4 Answers

Local Gravity (Black Hole) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges