Object rotation
Object rotation
What is the simplest way to rotate an entity a specigi number of degrees. Rotate sets it in motion but I need it to stop when fcing the camera
Re: Object rotation
If you want it done in a single frame and don't care about collisions in the mean time, you can just set the orientation matrix (or orientation quaternion, or world transform).
The specific properties that you could change are:
orientationMatrix
orientationQuaternion
worldTransform
internalOrientationMatrix
internalOrientationQuaternion
internalWorldTransform
If you do care about the collisions or want it to rotate over multiple frames, I'd recommend checking its progress each frame and updating the angular velocity accordingly.
For example, if you had a (normalized) direction A attached to an entity that you wanted to align with some other (normalized) direction B, you can take the normalized cross product of the two directions to get the axis of rotation C (C = A x B / ||A x B||). The amount of rotation could be found by computing Acos(A dot B).
Then you figure out what angular velocity you want it to move at. First, just pick some scalar value that seems right. To prevent overshoot, use the minimum between the rotation remaining (computed earlier) divided by the time being simulated or the scalar you picked. In other words, angularSpeed = Min(rotationRemaining, angularSpeed).
Multiply that speed by axis C and set the entity's angular velocity to that value.
There's a lot of different ways to go about doing this sort of thing depending on the behavior you want and what other requirements you have. The above is just one somewhat simple option.
The specific properties that you could change are:
orientationMatrix
orientationQuaternion
worldTransform
internalOrientationMatrix
internalOrientationQuaternion
internalWorldTransform
If you do care about the collisions or want it to rotate over multiple frames, I'd recommend checking its progress each frame and updating the angular velocity accordingly.
For example, if you had a (normalized) direction A attached to an entity that you wanted to align with some other (normalized) direction B, you can take the normalized cross product of the two directions to get the axis of rotation C (C = A x B / ||A x B||). The amount of rotation could be found by computing Acos(A dot B).
Then you figure out what angular velocity you want it to move at. First, just pick some scalar value that seems right. To prevent overshoot, use the minimum between the rotation remaining (computed earlier) divided by the time being simulated or the scalar you picked. In other words, angularSpeed = Min(rotationRemaining, angularSpeed).
Multiply that speed by axis C and set the entity's angular velocity to that value.
There's a lot of different ways to go about doing this sort of thing depending on the behavior you want and what other requirements you have. The above is just one somewhat simple option.
Re: Object rotation
Thanks
I'll give it a whirl.
Not sure I get the ||A x B || notation
will Vector3.Crossproduct() work here ?
it will always be the Up axis
I'll give it a whirl.
Not sure I get the ||A x B || notation
will Vector3.Crossproduct() work here ?
it will always be the Up axis
Re: Object rotation
The A x B / ||A x B|| just meant "A cross B divided by the length of A cross B." So yes, getting the cross product and normalizing it would work if you needed to do that. If it's always going to be the up vector though, that makes things easier
Re: Object rotation
one way I am trying is:
entity.orientationMatrix.Y = (float)rotation;
or
entity.orientationMatrix = Matrix.CreateRotationY((float)rotation);
This throws a compilation error, yet orientationMatrix has get/set properties.
What am I missing ?
entity.orientationMatrix.Y = (float)rotation;
or
entity.orientationMatrix = Matrix.CreateRotationY((float)rotation);
This throws a compilation error, yet orientationMatrix has get/set properties.
What am I missing ?
Re: Object rotation
There's a few things:
1: The orientationMatrix property returns a copy of the orientation matrix. If you take a field or property from the returned copy and try to set it, you're setting a 'worthless' value since it's not the original. The compiler checks this. For example:
generates an error:
Error 2 Cannot modify the return value of 'BEPUphysics.Entities.Entity.orientationMatrix' because it is not a variable....
2: The XNA Matrix class has no 'Y' field or property. If you meant 'Up,' then you need to give it a Vector3, not a float. I don't think this is what you are aiming for though since it doesn't produce a rotation.
3: Your second option:
works. This assumes, however, that rotation is a scalar. If rotation is a Vector3 or some other type that cannot be cast to a float, it will fail.
1: The orientationMatrix property returns a copy of the orientation matrix. If you take a field or property from the returned copy and try to set it, you're setting a 'worthless' value since it's not the original. The compiler checks this. For example:
Code: Select all
toAddBox.orientationMatrix.M11 = 1
Error 2 Cannot modify the return value of 'BEPUphysics.Entities.Entity.orientationMatrix' because it is not a variable....
2: The XNA Matrix class has no 'Y' field or property. If you meant 'Up,' then you need to give it a Vector3, not a float. I don't think this is what you are aiming for though since it doesn't produce a rotation.
3: Your second option:
Code: Select all
entity.orientationMatrix = Matrix.CreateRotationY((float)rotation);