Answer the question
In order to leave comments, you need to log in
How to simulate the trajectory of movement along an ellipse under gravity?
There are two bodies, one has a huge mass, the other has a single one. There is a code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class force : MonoBehaviour {
public Vector2 force1;
public Rigidbody2D myRigidbody;
public Rigidbody2D myRigidbody2;
public float f12;
public GameObject myObject1;
public GameObject myObject2;
public int nObject;
public float inputX;
public float inputY;
public float inputX2;
public float inputY2;
public float y;
// Use this for initialization
void Start () {
if (nObject == 1) {
myObject1 = GameObject.Find ("Sphere1");
myObject2 = GameObject.Find ("Sphere2");
myRigidbody = myObject1.GetComponent<Rigidbody2D> ();
myRigidbody2 = myObject2.GetComponent<Rigidbody2D> ();
}
if (nObject == 2) {
myObject1 = GameObject.Find ("Sphere2");
myObject2 = GameObject.Find ("Sphere1");
myRigidbody= myObject1.GetComponent<Rigidbody2D> ();
myRigidbody2= myObject2.GetComponent<Rigidbody2D> ();
}
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
if (nObject == 1) {
Vector2 ve = new Vector2 ();
ve.y = y;
ve.x = 0;
myRigidbody.AddForce (ve, ForceMode2D.Impulse);
}
Vector2 input = new Vector2 ();
input = myObject1.transform.position;
inputX = input.x;
inputY = input.y;
input = myObject2.transform.position;
inputX2 = 0;
inputY2 = 0;
float r12 = Mathf.Sqrt (Mathf.Pow (inputX2 - inputX, 2) + Mathf.Pow (inputY2 - inputY, 2));
float cosq12 = (inputX2 - inputX) / (r12);
float sinq12 = (inputY2 - inputY) / (r12);
float G = 7 * Mathf.Pow(10,-1);
f12 = (G * myRigidbody.mass * myRigidbody2.mass) * (Mathf.Pow (r12, -2));
force1 = new Vector2 ();
force1.x = f12 * cosq12;
force1.y = f12 * sinq12;
myRigidbody.AddForce(force1,ForceMode2D.Force);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question