Comments and answers for "Relative Movement & Momentum problem - Space sim"
http://answers.unity.com/questions/1276392/relative-movement-momentum-problem-space-sim.html
The latest comments and answers for the question "Relative Movement & Momentum problem - Space sim"Comment by Cresspresso on Cresspresso's comment
http://answers.unity.com/comments/1323994/view.html
Here is something that should head you in the right direction. It works for keyboard control. For use with the 'right thumbstick' you may need a separate script like the one [here][1]. If you get into trouble with input, I would make a new question and post about it there.
This implementation adds force in forwards and backwards directions based on the default input axis "Vertical" (up/down or w/s). The ship continues accelerating backwards even if it reaches zero (just like in real outer space).
void Update()
{
float force = engineForce * Input.GetAxis("Vertical");
// F = m*a = m*dv/dt therefore dv = F*dt/m
Vector3 deltaVelocity = transform.forward * (force * Time.deltaTime / mass);
velocity += deltaVelocity;
if (velocity.sqrMagnitude > 0.001f) // if velocity is not zero
{
// v = ds/dt therefore ds = v*dt
Vector3 deltaDisplacement = velocity * Time.deltaTime;
transform.position += deltaDisplacement;
}
}
This next implementation does not continue accelerating backwards if it reaches zero.
float force = engineForce * Input.GetAxis("Vertical");
// if force is forwards or velocity is forwards, add dv
if (force > 0 || Vector3.Angle(velocity, transform.forward) < 90f)
{
// F = m*a = m*dv/dt therefore dv = F*dt/m
Vector3 deltaVelocity = transform.forward * (force * Time.deltaTime / mass);
velocity += deltaVelocity;
}
if (velocity.sqrMagnitude > 0.001f) // if velocity is not zero
{
// v = ds/dt therefore ds = v*dt
Vector3 deltaDisplacement = velocity * Time.deltaTime;
transform.position += deltaDisplacement;
}
[1]: http://wiki.etc.cmu.edu/unity3d/index.php/Joystick/ControllerFri, 10 Mar 2017 05:23:30 GMTCresspressoComment by VAN-D00M on VAN-D00M's comment
http://answers.unity.com/comments/1321523/view.html
In my input section I turned engine force on and off and that would used to calculate the force. The switch would be, if ship is moving forward then pressing backwards/brake would switch engine force to negative 1 instead of 1. The way Ive got it set up is how I would do it.Sat, 04 Mar 2017 18:11:40 GMTVAN-D00MComment by bluewithenvy on bluewithenvy's answer
http://answers.unity.com/comments/1321521/view.html
would that be written in there as an if statment....im having trouble figureing how to write it out...correct me if im wrong though.Sat, 04 Mar 2017 18:03:27 GMTbluewithenvyComment by VAN-D00M on VAN-D00M's comment
http://answers.unity.com/comments/1321493/view.html
Negative your engine force? Stop subtracting the force when the ship stops.Sat, 04 Mar 2017 16:45:01 GMTVAN-D00MComment by bluewithenvy on bluewithenvy's answer
http://answers.unity.com/comments/1321213/view.html
Hey guys hoping this isn't a dead thread...
I have the F =m*a equation in my script but now I want to use it to slow and bring my ship to a complete stop when I'm pulling back on the right thumbstick. I don't want the ship to go in reverse...at least not at this time.
// F = m*a = m*dv/dt therefore dv = F*dt/m
Vector3 deltaVelocity = transform.forward * forwardInput * (moveSettings.engineForce * Time.deltaTime / physSettings.mass);
velocity += deltaVelocity;
// v = ds/dt therefore ds = v*dt
Vector3 deltaDisplacement = velocity * Time.deltaTime;
transform.position += deltaDisplacement;
}Sat, 04 Mar 2017 03:27:51 GMTbluewithenvyComment by VAN-D00M on VAN-D00M's comment
http://answers.unity.com/comments/1276693/view.html
Thanks a lot @SublimeGamer! We worked on it for a couple of hours and it started to make sense and finally worked. We weren't calculating velocity properly, as soon as we did it worked.Sat, 26 Nov 2016 19:11:26 GMTVAN-D00MComment by Cresspresso on Cresspresso's answer
http://answers.unity.com/comments/1276536/view.html
Are you sure you copied all of my code? In my CustomRigidbody class, I specified `velocity` as a Vector3, not a float. This represents the GameObject's velocity in 3 spatial dimensions, at this specific moment in time. I then added a different Vector3 to it, deltaVelocity (in the forward direction of the object). This means the world-space velocity is changed, not just the forwards velocity like your float is doing.
If you take my CustomRigidbody.cs class, exactly like it is in my answer, it works perfectly fine. Maybe you did something wrong in how you combined it with your old script?Sat, 26 Nov 2016 08:59:52 GMTCresspressoComment by VAN-D00M on VAN-D00M's answer
http://answers.unity.com/comments/1276471/view.html
Ok we've had time to try it out properly. We have recoded so we are actually calculating velocity correctly and then tried the code you supplied us with but it hasn't achieved the desired result. The ship is still carrying its' momentum in the direction its facing rather than when we rotate carrying on and "gliding" backward. If that makes sense @SublimeGamerSat, 26 Nov 2016 00:29:25 GMTVAN-D00MComment by VAN-D00M on VAN-D00M's answer
http://answers.unity.com/comments/1276426/view.html
Thanks for the response @Sublimegamer. I'm really excited to try out;
// F = m*a = m*dv/dt therefore dv = F*dt/m
Vector3 deltaVelocity = transform.forward * (engineForce * Time.deltaTime / mass);
velocity += deltaVelocity;
// v = ds/dt therefore ds = v*dt
Vector3 deltaDisplacement = velocity * Time.deltaTime;
transform.position += deltaDisplacement;
but it errors because velocity is a float and deltavelocity is a vector?
EDIT: Ignore that. I got round it by this but Im still restricted to movement on the Z. So I have that drift where I can turn around but I couldn't then rotate to face 'upwards' and add speed in that direction. If that makes sense.
Vector3 newVelocity = new Vector3(0, 0, velocity);
newVelocity += deltaVelocity;
EDIT: thinking about it I probably did that.Fri, 25 Nov 2016 22:10:03 GMTVAN-D00MAnswer by Cresspresso
http://answers.unity.com/answers/1276421/view.html
The built-in Rigidbody component is superior to custom scripts in almost every way when it comes to simulating physics in Unity. I would add a Rigidbody to the ship GameObject and use AddForce() to move the ship around. You just need to uncheck 'Use Gravity' in the Rigidbody's inspector, to simulate Zero G.
**Engine.cs**
using UnityEngine;
public class Engine : MonoBehaviour
{
public float engineForce;
void Update()
{
Rigidbody rb = GetComponent<Rigidbody>();
rb.AddForce(transform.forward * engineForce);
}
}
BUT, if you have a good reason not to use a Rigidbody, you could make your own sort of rigidbody script to handle velocity, like you were doing in your question.
**CustomRigidbody,cs**
using UnityEngine;
public class CustomRigidbody : MonoBehaviour
{
public float mass;
public float engineForce;
private Vector3 velocity;
void Update()
{
// F = m*a = m*dv/dt therefore dv = F*dt/m
Vector3 deltaVelocity = transform.forward * (engineForce * Time.deltaTime / mass);
velocity += deltaVelocity;
// v = ds/dt therefore ds = v*dt
Vector3 deltaDisplacement = velocity * Time.deltaTime;
transform.position += deltaDisplacement;
}
}Fri, 25 Nov 2016 21:50:03 GMTCresspresso