- Home /
Rotate rigidbody around point
I need a way to rotate a rigidbody around a specific point. The problem is, that it should react realistic and I can't use a fixed joint for it.
What I'm looking for is AddTorqueAtPoint(Vector3 torque, Vector3 position)
Of course this method isn't implemented, but there should be a way to rotate the rigidbody correctly by applying two (continuous) forces on the opposite sides of it, but I can't figure out how far away from the center and how strong these forces should be.
EDIT: I'll try to clarify what I'm trying to do. I have multiple objects that have the same parent. The parent has a rigidbody. I want to add a torque to one of the child objects so that it rotates to a specific rotation (using a maximum of force, so that it still interacts correctly with other objects)
The reason why I don't want to use different rigidbodies that are connected with joints it that the objects should stay absolutely fixed together. Connecting rigidbodies results in very strange vibrations in the objects, which makes it impossible to use for the game I'm working on.
EDIT2: Here is a small sketch... I hope that clears up what I'm trying to do. Thanks to those who already tried and thanks in advance to those who will (hopefully) help me later!
That wouldn't work because the rigidbody wouldn't be accelerated realistically. I want it to rotate with a specific force, not with a specific speed.
If I parent the rigidbody to a empty and rotate it, I will only be able to specify its speed, not the maximal rotation-force...
Answer by Bunny83 · Oct 27, 2012 at 09:29 PM
This is near to impossible with applying two forces blindly. The Physics system calculates the inerita tensor from all the rigidbody's colliders.(See Unity's inertiaTensor and inertiaTensorRotation) This tensor will affect how likely an object will rotate around a certain axis. Generally all forces applied to a rigidbody will result in a combination of a linear movement (velocity) and a rotation around the center of mass in one axis (angular velocity).
What you want is near spaceship science and they know every parameter of their ship. Besides that Physx is a physics simulation which just simulates real world physics, but the result is never accurate.
The best way might be to calculate a path to your target (great circle or elliptic) and just continuously set the rigidbody's velocity manually to the tangent vector of your path.
Thanks for that answer! I already was afraid that it would be a bit more complex than I thought at the beginning...
I think I'll describe what I'm trying to do a bit more detailed, maybe you'll be able to help me... Of course it isn't realistic at all, but I try to let it look as realistic as possible in the setting of the game. I'm making a RTS where you are playing on flying islands, that can be connected to expand the players territory. I have tried to connect the islands with fixed joints, but that caused strange vibrations which made the game unplayable in the end... what I thought of was to parent the islands to a empty and adding a rigidbody to the new parent (and also delete those of the individual islands..). I did add the masses of the islands together and it reacts still as it should.
Now here is where the actual problem is: The islands are moved by some sort of huge airship that is mounted on top of a single island. I want the player to be able to move their base to a specific point and with a specific rotation, but I want that the island complex is rotated much slower when the island-transportation-airship is mounted on a island that is further away from the center of mass. That's where I would need a "AddTorqueToPoint()" method...
As you can see that is a very basic feature in the game, so I would REALLY appreciate any help, be it a solution for my current system, or a alternative system that would work... Thanks for your help!
(1) if you want some unusual movement ... just animate it. it's that simple. just move the things where you want at the speed you want using some path system, or whatever works for you
(2) do what B said just animate the velocity as you wish
(3) FTR I still can't at all understand the set up here. Could you draw a diagram?
Unfortunately this critical sentence "but I want that the island complex is rotated much slower when the island-transportation-airship is mounted on a island that is further away from the center of mass" does not make sense to me, it has a superfluity of iss.
At one point you say "I want it to rotate slower .." [because of some game feature .. the airship image is placed elsewhere or whatever]
O$$anonymous$$, then just rotate it slower. Set the rotation to be slower.
Just include a screen shot or diagram perhaps for more help
It sounds like an interesting problem but there's a tradition on this site of questions being ... not quite like they seem. Halloween.
I didn't decribe that very well, of course it is independent of the position, but it's dependent of the mass. I don't want to just add the rotation, or animate it. Imagine you connect many islands in a row and rotate it around one island on the side. The island on the other end would have a unrealistically high speed. What I want is that the airship has a specific force to rotate islands... if you connect multiple islands the total mass is much higher, so the rotation speed would be much slower. The connected islands should still react as if they would be individual rigidbodies..
Of course if there would be a way to perfectly connect multiple rigidbodies, without any strange behavior, even when connecting many of them (like a big base that consists of large numbers of islands), that would completely solve the problem!
By the way, I can't upload a screenshot because I don't have any visuals for the connections yet, so it would probably just be confusing...
Your best alternative is to set the center of mass to your desired rotation point. $$anonymous$$eep in $$anonymous$$d that all rotations which might affect your whole island object are applied around the center of mass.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-centerOf$$anonymous$$ass.html
$$anonymous$$eep in $$anonymous$$d that the center of mass is in local space coordinates relative to the rigidbody's transform.
But would it still react the same way? It would also make it impossible to use multiple transporters, but I guess that would only cause problems anyway...
I think the best and easiest way to solve this problem is, that the transporter moves to the center between the islands. That way it would still be possible to rotate it normally!
Thanks for your help! Even if it's not the solution I wanted at the beginning, but at least you helped me to find a new solution that is probably even better from the players point of view!
Sometimes I'm just too focused on a specific problem, so that I don't see the easiest way any more...
Answer by Jack Al · Feb 08, 2013 at 11:38 AM
Hi, the question seems to be old enough, but perhaps my answer can be useful for later readers. I've had the same problem - needed to rotate the rigidbody around some center (other object). Had very bad days trying to calculate the physics on my own, but finally got it working with simple HingeJoint. You may have tried this, but I achieved to make it work without any vibrations of rotating object. You just need to make sure that:
you're not using any springs on your hinge joint (center)
the applied forces are perpendicular to the connecting vector of these objects (center and rotating object).
You don't need to change the gravity center of rotating object in this case. Just make your center's rigidbody NON-kinematic, but freeze all position axes. Also freeze the rotation axis of HingeJoint's enabled axis. I.e. if you have enabled axis X on hinge joint, freeze this in rigidbody.
Now your center object will rotate around itself (this doesn't matter) in all axes except the one that makes the orbital object rotate.
This way you have object that can rotate around that center in all 360 degrees (like electrons around the core of atom).
In particular case, if you need to make it rotate on plane (like planets around the star), just make your center Kinematic and enable the axis (axes) around which your orbital object will rotate.
Answer by Epixam · Oct 27, 2012 at 05:18 PM
Perhaps a fix could be for you to create an empty game object at the point in which you would like to rotate around and set it as the parent of your objects transform and apply the torque to that object. When OnCollisionEnter is called destroy the empty object?
The problem is that I want that the rigidbody continues to rotate around the point while colliding, although it should be (realistically) slowed down. Your approach would immediately stop the rotation.
Aaah I see. Is the point you would like to rotate around static in reference to the main object or will it be changing?
Unfortunately it will change... maybe even while rotating!
Do you always have the exact location of this point that you would like to rotate around?
Answer by Montraydavis · Oct 27, 2012 at 03:00 PM
Set the contraints on the Rigidbody ( freeze pos(x,y,z) rot(x,z) ) and AddForce from the Docs. -- http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
PS: Alter the settings to your requirements.
Have you really read the question? $$anonymous$$aybe I haven't formulated it well enough... I want that the rigidbody still reacts to collisions with other rigidbodies.
Also the whole point of this question is that I want to rotate the rigidbody around a point that is NOT the center point, so constraining the rigidbody would make everything worse...