Hello!
I made a little scene to make a gravity system that allow to have planets as gravity centers and even a Solar System (i hope). After a little bit more than a week of thinking and trying multiple things i finally took the gravity formula and scripted it to make my objects attract each other according to their mass and distance from each other. (in other words: applying the basics of Newtonian laws)
It works pretty well, i'm quite satisfied and will continue to work on it to make it work more like "real world" and then i plan to make games with it. I made it in 2D because... Why not! I love 2D and want to use this gravity system in 2D (it basicly changes nothing anyway, the problem doesn't come from that).
My problem is that when you have two objects of significantly different masses, the script on the bigger one gives multiples errors because the force applied to it is so low the engine can't measure it (at least that's how i understood the error) and the console is flooded in errors.
**Here are all the details about my project (it's really basic, i just made very simple tests).**
*Here you have the very light object named "body". When i launch the scene it is given a force of 50 upward on the screen so it starts spinning around the Black dot (which is a -very, very veeery symbolic- planet)*
![alt text][1]
*and here you have the planet with it's 1000 mass to make it stay in the middle of the scene.*
![alt text][2]
[1]: /storage/temp/57978-body.png
[2]: /storage/temp/57979-planet.png
It works well, the white little square spins around it in a stable orbit and the Planet has very tiny forces aplied to it that makes it move a little (it's transform has a value multiplied by powers of ten -sorry if it's not the right words, i'm not english and really don't know how to say that...- so the move is not even noticable). I think it's not perfectly exact in the eyes of sciences at all but i don't really care, it looks accurate enough to me and anyone (i think).
*Here is the Error.
In a few seconds i have 999+ of it as it seems to be sent almost every frame...*
Rigidbody2D.AddForce(force) assign attempt for 'Planet' is not valid. Input force is { NaN, NaN }.
UnityEngine.Rigidbody2D:AddForce(Vector2)
gravityScript:FixedUpdate() (at Assets/Scripts/gravityScript.cs:37)
*And finally here is my script (the only one in the project so far)*
using UnityEngine;
using System;
public class gravityScript : MonoBehaviour
{
public GameObject[] Attraction;
public float vel;
public float acceleration;
// Use this for initialization
void Start ()
{
gameObject.GetComponent().AddForce(new Vector2(0.0f,vel)); //This line just gives a force to an object if you want to. Setting the public float vel to 0 will make it start normally.
Attraction = GameObject.FindGameObjectsWithTag("Bodies");
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
foreach(GameObject body in Attraction)
{
float massA = gameObject.GetComponent().mass; // the game object mass
float massB = body.GetComponent().mass; //the "other" object mass
float dcarré= Mathf.Pow(Vector2.Distance(gameObject.transform.position, body.transform.position),2); // distance between the objects powered by two (again, sorry if i say it wrong)
float formule = acceleration * (massA* massB )/dcarré; //Newtonian gravity formula as i found it on a highschool website.
body.GetComponent().AddForce((gameObject.transform.position - body.transform.position ).normalized * formule); // direction to apply the force and then the formula
// I did it step by step to make it clearer for myself and anyone who would read it. I think it's easy to go through it like that ;)
}
}
}
**So, finally, here is my question:**
How could i just ignore that error? My project works as i want and this error doesn't seem to have very much impact on anything. If you think i should solve it i'd be grateful to know why it's important and how to do so but my point here is that i just want to get rid of it and go on! xD
Thanks a lot for reading and giving any help. Oh and by the way... How do you english speaking people say "puissances de 10"? :P
↧