- Home /
Rigidbodies with fixed joint connection rotate instead of forward/back
Here's the situation. I have a scene with no gravity, and a ship that I move with add relative force/torque. It's all Newtonian physics, so equal and opposite reactions are the order of the day. It's not perfect, but with my very small time constraints it's good enough.
Problem: I have a rigidbody cargo that you pick up with a fixed joint. So envision a ship, with a container underslung. The connect/disconnect works like a charm. Now, we push forward thrust, and go into front flips. Back gives the same result, only back. My other movements, pitch, roll, yaw, transverse all mostly work. They have slight drift in odd angles but I assume it's all related.
I've played with masses and the other rigidbody settings but don't seem to be resolving the issue.
I doubt it's something currently in my code, but if anyone wants a detail I will provide it.
Here's the thrust code in any case, and yes I know what the directions are, I had some weird kludgery but this does otherwise work like a charm.
if(Input.GetButton("Prograde")){ //Accel
rigidbody.AddRelativeForce (Vector3.left * thrustSpeed / 4);
fuel -= thrustSpeed / 20;
}//end pro
if(Input.GetButton("Retrograde")){ //Decel
rigidbody.AddRelativeForce (Vector3.right * thrustSpeed / 4);
fuel -= thrustSpeed / 20;
}//end retro
Removed .normalize, and I assume you mean something like,
In Update()
if(docked){
direction = (cargo.transform.TransformPoint(cargo.rigidbody.centerOf$$anonymous$$ass)+transform.transform.TransformPoint(rigidbody.centerOf$$anonymous$$ass))/2;
}
I did this, and tried. I touched the prograde key at the lowest thrust setting and I spun into the roof so quickly I barely had time to see before instantly fatal impact. Whoops. Seeing as that seems to have made it worse, is there something else I'm missing? Don't think there's any other relevant code.
Answer by Jeff-Kesselman · May 03, 2014 at 09:30 PM
Attaching the cargo moves your center of gravity. So now, when you put a force on the ship, its off center and it makes it flip. This is exactly what would happen in space without correctional forces :)
Either determine the new center of gravity and point your acceleration force at it, OR set your cargo mass (including the arm picking it up) to 0.
$$anonymous$$mmm. I suspected that might have been the problem, i.e. what you said about the center of gravity, but as I'd said before I messed with masses to no avail. However, after learning that mass doesn't allow 0, I set it to something equivalently tiny and was pleased to see it work. The only trouble is, a mass that low plays havoc with collisions, which is why I wanted fixed joint in the first place. I played around a bit but nothing with enough mass to do solid collisions didn't adversely effect pro/retrograde movement.
So, not quite sure how I'd go about adjusting the force to the new center, tips?
Sure. If you have 3 connected bodies, of equal mass, then the center of mass of the entire fixture is (centerofmass1+centerofmass2+centerofmass3)/3.
If they aren't of equal mass then you need to weight the calculation with the mass: ((cenertofmass1*mass1)+(centerofmass2*mass2)+(centerofmass3*mass3)/(mass1+mass2+mass3)
In either case this will give you a Vector3 that is the proper center of mass.
Use this as the position with "AddForceAtPosition"
$$anonymous$$mmm. Seems like it should work, but I suspect there's an orientation issue co$$anonymous$$g up. It seems like the container's direction has something to do with it, because now I'm getting various different rotations ins$$anonymous$$d. I can't be sure which direction the container will be when the player picks it up, so not sure what do with this. Telling the container to rotate to ship.transform.rotation on dock had the unfortunate side effect of catapulting me clear out of the hangar.
I have a plan B if I can't get this to work, but it seems much more realistic.
How are you getting the centers of mass, from https://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-centerOf$$anonymous$$ass.html ?
That returns the center of mass relative to the local origin. So you would need to use gameObject.transfrom.TransformPoint(gameObject.rigidbody.centerOf$$anonymous$$ass) to get the CO$$anonymous$$ in world space, which is what you need.
$$anonymous$$mmm, well, that isn't how I was doing it, but I get the same result with that in ins$$anonymous$$d.
direction = (cargo.transform.TransformPoint(cargo.rigidbody.centerOf$$anonymous$$ass)+transform.transform.TransformPoint(rigidbody.centerOf$$anonymous$$ass))/2;
With my movement code as:
if(Input.GetButton("Prograde")){ //Accel
if(!docked){
rigidbody.AddRelativeForce (Vector3.left * thrustSpeed / 4);
}
else{
rigidbody.AddForceAtPosition (direction.normalized,Vector3.left * thrustSpeed / 4);
}
//audio.PlayOneShot(clipEngine, 0.5);
fuel -= thrustSpeed / 20;
}//end pro
if(Input.GetButton("Retrograde")){ //Decel
if(!docked){
rigidbody.AddRelativeForce (Vector3.right * thrustSpeed / 4);
}
else{
rigidbody.AddForceAtPosition (direction.normalized,Vector3.right * thrustSpeed / 4);
}
fuel -= thrustSpeed / 20;
//audio.PlayOneShot(clipEngine, 0.5);
}//end retro
So, what am I doing wrong?
Your answer
Follow this Question
Related Questions
How to set velocity to a previouse value 1 Answer
Slow down a character while they are midair while keeping their original velocity 0 Answers
Changing 1 parameter of rigidbody.velocity (Vector3) 2 Answers
What is wrong with this rigidbody? 2 Answers
How would I counteract the force of the velocity and set it to zero after the input has stopped? 3 Answers