- Home /
Predicting path line affected by planet gravity.
Hey guys,so I have a question for you.
First of all, I should describe a main concept. It's pretty simple, if you played Angry Birds space, it pretty obvious. I need make a prediction path that interact with colliders or/and with planets gravity. I have several types of planets, ones that attract objects, and the others that repel them. I have several script for all of this. 1.Scripts for planets repel/attract 2.Script for predicting path 3.Script for instantiation and slingshot. So, I know how to predict path with normal, static gravity like -9.8f for cases in your scene. But what about if you have a planet, and each object that trigger the collider of planet(kinda atmosphere) start being attracted to this planet, with acceleration, radial velocity etc. When object is out of triggers there is no any gravity at all, so predicting path line will remain straight line, not a bid deal. Her's some of my scripts.
Planet attraction:
private float gravityFactor = 1.0f;
private bool tri = false;
GameObject planet;
void Start () {
}
public void OnTriggerEnter(Collider collision) {
planet = collision.gameObject;
if(collision.CompareTag("star")) {
tri = !tri;
}
}
public void OnTriggerExit(Collider collision) {
tri = !tri;
}
void FixedUpdate () {
if(GameObject.Find("planet(Clone)")
!= null) { if( tri == true){ Vector3 pos = ((transform.position - planet.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (transform.position - planet.gameObject.transform.position).sqrMagnitude);
planet.gameObject.rigidbody.AddForce(pos,
ForceMode.VelocityChange);
}
}
}
And prediction path script:
private Vector3 StartVelocity = new Vector3(-5, 5, 0);
public float PredictionTime;
private Vector3 G;
Vector3 scale = new Vector3 (1.5f,1.5f,1.5f);
float scaleMult = 1f;
int numb = 1;
private bool tri = false;
private GameObject star;
private float gravityFactor = 1.0f;
GameObject[] pnts = new GameObject[60];
void Awake() {
Vector3 momentum = StartVelocity;
Vector3 pos = gameObject.transform.position;
Vector3 last = gameObject.transform.position;
}
void Start() {
if(G == Vector3.zero) {
//a hacky way of making user this gets initialized in editor too...
//this assumes 60 samles / sec
G = new Vector3(0, -9.8f, 0) / (PredictionTime * 60f);
}
}
public void OnTriggerEnter(Collider collision) {
star = collision.gameObject;
if(collision.CompareTag("star")) {
tri = !tri;
}
foreach(GameObject sph in pnts) {
if(tri){
G = ((star.gameObject.transform.position -
sph.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (star.gameObject.transform.position - sph.gameObject.transform.position).sqrMagnitude); sph.gameObject.transform.position += G; } }
}
public void OnTriggerExit(Collider collision) {
tri = !tri;
}
void Update() {
if(Input.GetMouseButtonDown(0)) {
Vector3 momentum = StartVelocity;
Vector3 pos = gameObject.transform.position;
Vector3 last = gameObject.transform.position;
for(int i = 0; i< (int) (PredictionTime * 15); i++) {
momentum += G;
pos += momentum;
GameObject sph = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sph.gameObject.transform.position = new Vector3(last.x, last.y, 0);
sph.collider.isTrigger = true;
sph.transform.localScale = scale;
sph.gameObject.name = numb.ToString();
scale *= scaleMult;
scaleMult-= 0.005f;
last = pos;
Debug.Log (numb);
numb+=1;
pnts[i] = sph;
if(tri == true) {
G = ((star.gameObject.transform.position -
sph.gameObject.transform.position).normalized rigidbody.mass gravityFactor / (star.gameObject.transform.position - sph.gameObject.transform.position).sqrMagnitude); momentum +=G; pos += momentum; sph.gameObject.transform.position = last; last = pos; }
}
}
if(Input.GetMouseButtonUp(0)) {
for(int i = 0; i< (int) (PredictionTime * 30); i++) {
Destroy (GameObject.Find(numb.ToString() +
"(Clone)")); numb-=1; } } }
So, what I should do with predicting path line so it cab be affected by planets gravity. Thanks for your responses, guys.
Answer by Scribe · Jan 30, 2015 at 12:13 PM
Check the edit I made to my answer HERE
You can produce effects such as:
Though it assumes your gravitational objects are stationary and will not effect each other. Also look in the comments for ways to optimise it to work better at run-time over several frames.
Hope that helps,
Scribe
Your answer
Follow this Question
Related Questions
Rigidbody gravity between planets 1 Answer
gravity causes position.y fluxuation sitting still 0 Answers
Moving selected unit to location in space rts based on screen touch 3 Answers
Trying to rotate player, how? 0 Answers
Drift Rotation 1 Answer