Simulate Gravity & Orbit in Unity - c#

Hey and thanks for your help in advanced.
I've watched a few youtube videos on how to add Solar System and orbiting Gravity in Unity and ended up using the this for help for the solar system gravity part.
https://www.youtube.com/watch?v=Ouu3D_VHx9o&t=114s&ab_channel=Brackeys
But right after i decided to trying to make my planet orbit the sun i used this Wikipage for the math equation
https://en.wikipedia.org/wiki/Orbital_speed
But for some reason either my planets flies away of the sun start flying towards the planet. I've been looking around for 2 days and can't seem to make it work and tried diffrent type of possiblies.
Here is my code
public class Planets : MonoBehaviour
{
const float G = 100F;
public Rigidbody rb;
public float CurrentV;
private void FixedUpdate()
{
Planets[] attractors = FindObjectsOfType<Planets>();
foreach (Planets AllPlanets in attractors)
{
if (AllPlanets != this)
{
Orbiting(AllPlanets, CurrentV);
Attract(AllPlanets);
}
}
}
void Attract(Planets objToAttract)
{
Rigidbody RbTpAttract = objToAttract.rb;
Vector3 direction = rb.position - RbTpAttract.position;
float distance = direction.magnitude;
float ForceMagnitude = G * (rb.mass * RbTpAttract.mass) / Mathf.Pow(distance, 2);
Vector3 Force = direction.normalized * ForceMagnitude;
RbTpAttract.AddForce(Force);
}
void Orbiting(Planets objToAttract, float CV)
{
Rigidbody RbTpAttract = objToAttract.rb;
Vector3 direction = rb.position - RbTpAttract.position;
float distance = direction.magnitude;
float ForceMagnitude = Mathf.Sqrt((G * rb.mass) / (2 / distance - 1 / RbTpAttract.mass));
Vector3 Force = direction.normalized * ForceMagnitude;
RbTpAttract.velocity += Force;
}
}

The problem is that the formula for orbital speed is used to derive the speed of an object in orbit, but you're using it as a form of constant thrust applied to each body towards each other. That's a bit like calculating the speed of a moving car, and then applying the same speed back to it as an impulse!
The only force experienced by objects in orbit is the one you get from Newton's law G * m * m / r*r. In order to actually orbit though, the planets will need an initial velocity - this can be calculated from the orbital speed formula. Calculate it at the given distance, and apply it on Start() in a direction perpendicular to the orbital plane and the direction to the sun (or whatever you want to orbit), you can get this from dir = Vector3.Cross(sunDir, Vector3.up).normalized
Note that gravitational systems are not numerically stable in physics engines relying on euler integration (such as PhysX). You need things like Runge-Kutta integration for that, or the planets will eventually lose their orbit if you leave the simulation running for long enough.

Related

How to tilt an object depending on the direction of the player

How do I correctly calculate the rotation angle so that the object dodges the player? It means that whichever side the player comes from, the object must turn away from the player depending on its direction.
I want the effect like in the video but without Joint, only rotation angle: https://www.youtube.com/watch?v=GJhiR3SOyXs
I need it for spherical map. And right now it looks like this.
calculate the direction Vector from player to the object.
calculate the axis of rotation by calculating the cross product of the direction Vector and the world up. Doing so creates a vector that is orthogonal to both, which is what we need.
calculate the angle by how much to rotate. This is done by clamping the distance (magnitude of direction) from 0 to the maximum effect distance and dividing it by the effect distance. Doing so creates a value from 0 to 1. However we need a value from 1 to 0, so there is no effect when the player is far away from the object and the maximum when close. To do so you simply subtract the initial value from 1. By multiplying the result with the max angle we calculate an angle in the range of 0 to maxAngle.
Finally we calculate the object rotation by multiplying the initial rotation with the rotation around the axis.
[SerializeField] Transform player;
[SerializeField] float effectMaxDistance=1;
[SerializeField] float maxAngle=50;
Quaternion initialRotation;
void Start(){
initialRotation = transfrom.rotation;
}
void Update(){
Vector3 dir = player.position - transform.position;
Vector3 axis = Vector3.Cross(dir, Vector3.up);
float angle = (1-(Mathf.Clamp(dir.magnitude, 0 effectMaxDistance) / effectMaxDistance)) * maxAngle;
transform.rotation = initialRotation * Quaternion.AngleAxis(angle, axis);
}
Note that cross products and Quaternion multiplications are not commutative and need to be done in this exact order!
Bonus answer:
If you're on a sphere you need to use the normal of the ground as the up direction. Since you've probably initially rotated the tree the right way up, you could do the following:
[SerializeField] Transform player;
[SerializeField] float effectMaxDistance=1;
[SerializeField] float maxAngle=50;
Quaternion initialRotation;
Vector3 initialUp;
void Start(){
initialRotation = transfrom.rotation;
initialUp = transfrom.up;
}
void Update(){
Vector3 dir = player.position - transform.position;
Vector3 axis = Vector3.Cross(dir, initialUp);
float angle = (1-(Mathf.Clamp(dir.magnitude, 0 effectMaxDistance) / effectMaxDistance)) * maxAngle;
transform.rotation = initialRotation * Quaternion.AngleAxis(angle, axis);
}

How to get the signed angle of a quaternion on an arbitrary axis

I'm trying to make an airplane controller, I am kind of aiming for something between arcade and realistic, so I want the plane to turn with a force proportional to the roll.
I haven't coded in any adjustments and I'm still prototyping the whole thing, but I encountered a problem with getting the signed rotation angle while using quaternions, I had a look at Determining if quarternion rotation is clockwise or counter clockwise here on SO but I am having trouble generalizing the solution to the (almost) arbitrary plane the rotation can be at.
What I made by now:
private void FixedUpdate()
{
float desiredYaw = _yaw * _rotationSpeed * Time.fixedDeltaTime;
float desiredPitch = -_pitch * _rotationSpeed * Time.fixedDeltaTime;
float rotationStepSize = _throttle * Time.fixedDeltaTime;
Quaternion toRotate = Quaternion.Euler(desiredPitch, 0, desiredYaw);
Quaternion straighRotation = Quaternion.LookRotation(_transform.forward, Vector3.up );
_rotation = _transform.rotation * toRotate;
float turningForce = Quaternion.Angle( _rotation, straighRotation );
_rigidbody.MoveRotation( _rotation );
_rigidbody.AddTorque( turningForce * _rotationForce * rotationStepSize * Vector3.up );
_rigidbody.AddRelativeForce( _speed * rotationStepSize * Vector3.forward );
}
EDIT: I realized I'm calculating the turning force using the roll rather then the yaw, that was intended just wrong wording, corrected now.
Since all you need is a factor that describes how downward the plane's right is, you can just use the y component of the plane's right for that. No need to bring in quaternions or even trigonometry. Explanation in comments:
private void FixedUpdate()
{
// ...
// Calculate how downward local right is in range [-1,1]
// The more downward, the more tilted right the plane is
// positive = tilted right
// negative = tilted left
float turnFactor = -_transform.right.y;
// Could do things to modify turnFactor to affect easing here.
// For instance, if turning rate should start slower then rapidly increase:
// turnFactor = Mathf.Sign(turnFactor) * turnFactor * turnFactor;
// Use factor and _rotationForce member to calculate torque, apply along
// global up.
// We expect to call this every fixed frame so we can just use the default
// ForceMode of ForceMode.Force which multiplies fixed delta time inside.
_rigidbody.AddTorque(_rotationForce * turnFactor * Vector3.up);
// ...
}

Projectile shooting with custom force

I have made a projectile shooting system. I want to shoot an object from position A to B following an indicated path. Everything is working fine except for one thing. The velocity applied on the object is calculated based on the distance between A and B, the value of time is 1 to travel this distance. Meaning that the farther I hit, the quicker it goes. I want to have control of the force applied. Meaning that it should go with my set speed whether I hit near or far. Tried normalizing the velocity and multiplied it by my custom force value, but then it moves away from its trajectory.
(See this link below, no matter how close or far we hit the object. It goes with the same speed while following the trajectory indicated. I want to develop this functionality.
https://play.google.com/store/apps/details?id=com.ghakilo.trickytrack)
Vector3 calculateVelocity(Vector3 target, Vector3 origin, float time)
{
Vector3 distance = target - origin;
Vector3 distanceXZ = distance;
distanceXZ.y = 0f;
float Sy = distance.y;
float Sxz = distanceXZ.magnitude;
float Vxz = Sxz / time;
float Vy = 0f;
Vy = Sy / time + 0.5f * Mathf.Abs(Physics.gravity.y) * time;
Vector3 result = distanceXZ.normalized;
result = result * Vxz;
result.y = Vy;
return result;
}
Physics time!
Velocity is a vector whose magnitude is speed.
If you want to fix the speed at which your projectile starts, that only leaves the direction of the velocity in your hands, so you need to calculate the direction in which you want to yeet your projectile.
If you're like me, you shoot straight at the target, so you'd set the direction vector to go from the origin to the target. Which is simply target - origin, or what you calculate as distance. I'm going to call this direction because that's what it really is being used for.
Now to use this as the direction vector for your velocity, convert distance to a unit vector (not sure how you do this in the unity framework, but direction.Normalize()?)
Then multiply this by the speed to get your velocity vector!
Vector3 calculateVelocity(Vector3 target, Vector3 origin, float speed)
{
Vector3 direction = target - origin;
direction.Normalize();
Vector3 result = direction * speed;
return result;
}
First add rigidbody to your bullet and set the gravity scale to .5
and add this code to your bullet prefab
Vector3 calculateVelocity(Transform target, Transform origin, float time)
{
Vector3 direction = target.transform.position - origin.transform.position;
float distance = Vector3.Distance(origin.transform.position, target.transform.position);
result = direction.normalized * distance * speed * Time.deltaTime;
return result;
}

Stabilize hovercraft rigidbody upright using torque

I'm currently creating a game involving a hover-bike. when the bike collides with something , it's angles change naturally. I wish to create some sort of way for it to tend back to 0. Here's what I've tried:
if (hoverbike.rotation.x != 0 || hoverbike.rotation.z != 0)
{
hoverbike.AddTorque(x: Mathf.MoveTowardsAngle(hoverbike.rotation.x, 0, 0.01f), y: hoverbike.rotation.y, z: Mathf.MoveTowardsAngle(hoverbike.rotation.z, 0, 0.01f));
}
transform.Rotate(0.0f, -Input.GetAxis("Mouse X") * 0.5f, 0.0f);
It's hard to explain what it's doing because I don't understand what it's doing, it just seems to spin out.
Here's the rest of my code if interested: https://pastebin.com/kzMDQMVF, it's a mess but I'm still learning how to use Unity.
Oh and angle y shouldn't tend to 0 because that's the horizontal angle.
You can determine the quaternion that would rotate the hovercraft from its current up to world up using Quaternion.FromToRotation:
Rigidbody hoverRB; // hovercraft's rigidbody
Quaternion deltaQuat = Quaternion.FromToRotation(hoverRB.transform.up, Vector3.up);
Then use Quaternion.ToAngleAxis to convert that to an angle & axis:
Vector3 axis;
float angle
deltaQuat.ToAngleAxis(out angle, out axis);
Then, cancel out some of any existing rotational velocity so that you'll eventually reach the goal:
float dampenFactor = 0.8f; // this value requires tuning
hoverRB.AddTorque(-hoverRB.angularVelocity * dampenFactor, ForceMode.Acceleration);
And then apply some torque along the axis we found before, scaled by how much angle remains:
float adjustFactor = 0.5f; // this value requires tuning
hoverRB.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);
Any conversion between radians Rigidbody uses and degrees of ToAngleAxis is redundant with the float constants, so don't worry too much about it.
Make sure this is all being done in FixedUpdate (or a function called/running in FixedUpdate time) due to how the torque's direction will likely need to change from one physics step to another. So, altogether:
Rigidbody hoverRB; // hovercraft's rigidbody
...
void FixedUpdate()
{
Quaternion deltaQuat = Quaternion.FromToRotation(hoverRB.transform.up, Vector3.up);
Vector3 axis;
float angle
deltaQuat.ToAngleAxis(out angle, out axis);
float dampenFactor = 0.8f; // this value requires tuning
hoverRB.AddTorque(-hoverRB.angularVelocity * dampenFactor, ForceMode.Acceleration);
float adjustFactor = 0.5f; // this value requires tuning
hoverRB.AddTorque(axis.normalized * angle * adjustFactor, ForceMode.Acceleration);
}

Getting a direction from rotation around the Z

I have a plane. It is rotated 180 degrees on the Y , his position is 0,0,0 he is facing the X axis. That means that I rotate it around the Z (Euler angles) to change the direction it is facing. I have clouds the clouds have a movement script. I want the clouds to move in the opposite direction of the direction the plane is facing at the Y axis and X axis.
For example when the plane's rotation.eulerAngles.Z = 0. The clouds should move at full speed toward minus X. And when rotation.eulerAngles.Z = 90 the clouds should move at full speed towards minus Y. When rotation.eulerAngles.Z = 45 the clouds should move at half speed towards minus X and at half speed towards minus Y and so on.
Here's two illustrations to make it easier for you to visualize :
illustration 1
illustration 2
Here's a photo of the scene again to make it easier for you to visualize:
the scene
The idea is to create the illusion that the plane is moving, actually moving the plane in this scene will make a lot of problems that I really don't want to deal with.
Current cloud movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCloud : MonoBehaviour
{
public float speed;
void Update()
{
var plane = GameObject.Find("plane");
var dir =(plane.transform.position - plane.transform.forward)*speed*Time.deltaTime ;
transform.position = new Vector3(transform.position.x + dir.x , transform.position.y + dir.y , transform.position.z);
if (transform.position.x < -140)Destroy(gameObject);
}
}
So how would you approch this problem ?
With unity.
Edit:
I think trigo may be helpful here using a linear function
here's my new code :
public float speed;
void Update()
{
var plane = GameObject.Find("plane");//Get the airplane
var slope = Mathf.Tan(360 - plane.transform.rotation.eulerAngles.z);//Degrees to a slope (how much y it does in one x)
var y = 1*slope;//kinda the very definition of the slope...
var x = 1/slope;//math
transform.position += new Vector3(x,y,0)*Time.deltaTime*speed;//the 1 , 1 point of our linear function(our direction) * delta * speed
if (transform.position.x < -140)Destroy(gameObject);
}
currently the clouds just shake all over the scene. sometimes they do finally go to the right direction but it's not idle.
Would like to get a second opnion on my math.
How I solved the problem :
I just did
transform.position += GameObject.Find("plane").transform.right * Time.deltaTime * speed;
Thanks to PrinceOfRavens for his help on finding this out
does the blue axis(z) of the airplane face forward? with the code you have implemented? what direction do the clouds move?
forward in unity is the red axis (z). you code should work if the plain is oriented correctly. find the axis thats coming from the nose, and note the color.
if its :
blue you want -transform.right,
green you want -transform.up

Categories

Resources