How to calculate physics through script
How I can calculate everything in script manually ?
What im doing is manually applying physics to gameobject through script (I dont want to use rigidbody) So I set shell initial velocity based on force that is pushing shell out of cannon and then shell trajectory is calculated using its velocity, gravity, drag, mass, spin
Velocity = speed + direction but if I set in unity initial velocity to be velocity = direction * force is it right ?
How do I set angular velocity for example (I fire tank shell that is rotating at some speed and how do I apply rotation to gameobject (gameObject.transform.rotation = angularVelocity * fixedDeltaTime) ?
Is it correct to rotate gameObject to its direction ? (in this case tank shell)
How I can calculate drag based on air resistance, shell spin ? (and also decrease shell spin over time)
I still dont know what is difference between angular momentum and angular velocity so if someone explains it to me I will be glad
heres how im doing it now
public struct ProjectileData
{
public readonly float mass, drag, force;
public Vector3 origin;
public Vector3 velocity;
public Quaternion rotation;
public ProjectileData(Vector3 _origin, Vector3 _velocity, Quaternion _rotation, float _mass, float _drag, float _force)
{
this.mass = _mass;
this.drag = _drag;
this.force = _force;
this.origin = _origin;
this.velocity = _velocity;
this.rotation = _rotation;
}
}
public class C_BaseProjectile : MonoBehaviour
{
public ProjectileData projectile;
public C_BaseProjectile(ProjectileData _projectile)
{
this.projectile = _projectile;
}
public void UpdateVelocity()
{
this.projectile.velocity += Physics.gravity * Time.fixedDeltaTime;
this.projectile.velocity *= (1 - Time.fixedDeltaTime * this.projectile.drag);
}
public void UpdatePosition()
{
this.projectile.origin += this.projectile.velocity * Time.fixedDeltaTime;
}
public void UpdateRotation()
{
this.projectile.rotation = Quaternion.FromToRotation(Vector3.forward, this.projectile.velocity);
}
}
Answer by tylerdtrudeau · Mar 13, 2021 at 06:08 AM
Well first off, you are asking some questions that will take some serious math to figure out. Ill start with asking a few questions. Does the spin of the shell really have to slow down through its flight path? Im asking this because although it is hyper realistic, will it affect your game? will anyone even notice? will it affect the flight path? Even in games like COD I dont know if they would go that far for the physics of it. As for some of your other stuff, gravity is easy. -9.81 m/s^2 in the Y direction (or Z pending on how youre oriented) or just use "velocity = Physics.gravity * Time.deltatime;" as for drag/ air resistance, youll need to find some resources that talk about the shape/ drag coefficient for shells. Luckily im sure this isnt very hard to find if you look around online. You could look in the forums of gun websites when people are talking about the calculations for doing longshots, then just scale it up (mass, frontal area, etc..) for the size of your shell and im sure it would be close enough for your purposes. Back to the rotation, I personally would just apply a rotation along the shells longest axis when its fired and leave it at that, if you really want you can apply an initial angular rotational speed then apply an angular velocity in the opposite direction and make it act on the shell over time to simulate the rotation slowing. Also keep in mind, I am an Engineer, so I have done lots of calculations of this nature before (aerodynamics not actually bullets or shells) but I'm guessing most people here might not be familiar with the math that goes into this (im assuming most would use the built in physics) This is the type of stuff youre going to have to figure out for yourself, do the research and if you are having trouble with implementation then come back and ask for help with that. Sorry but im not about to spend hours opening up my old textbooks and crunching the numbers just becuase you dont want to.