Capsule movemet Matrix

Discuss any questions about BEPUphysics or problems encountered.
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Capsule movemet Matrix

Post by alfarza »

Hi Norbo ... :D
first thing can the capsule rotate or Y axis , because I need to rotate my player when capsule rotating ?
second thing i'm trying to Know how "capsule.worldTransform" work (what type of matrix works )?
I need from capsule to move forward and backward on Z axis and straf to left and right on X axis and rotate on Y axis and but this line in the WorldTransform (but i don't know how it's work) ?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

first thing can the capsule rotate or Y axis , because I need to rotate my player when capsule rotating ?
A default capsule can rotate around any axis. If you lock the rotation by setting the inertia to infinity, it cannot rotate due to forces. You can still set its orientation manually through the properties like Orientation, though.

However, I would recommend handling all player rotation in game logic rather than rotating the physical capsule itself. You can consider the player to be aiming in any direction regardless of what the orientation of the capsule actually is.
how "capsule.worldTransform" work (what type of matrix works )?
The WorldTransform property is a convenience property that takes a rigid transformation in an XNA Matrix format. A "rigid" transform includes only position and orientation, no shear, scaling, etc. Unless you have find it convenient to use the Matrix representation of the world transform, you don't need to use the WorldTransform property. Generally, when setting the values, it's easier to just use the Position and Orientation properties directly. Setting those will change the read values of the WorldTransform if you're using it for graphics.
I need from capsule to move forward and backward on Z axis and straf to left and right on X axis and rotate on Y axis and but this line in the WorldTransform (but i don't know how it's work) ?
Check out the SimpleCharacterControllerInput class in the BEPUphysicsDemos. It takes input and the camera's matrix to pick movement directions and strafe/move around.
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

this is my new code :
playerRotationY -= padState.ThumbSticks.Right.X *MathHelper.ToRadians(2f) ;
playerCapsule.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, playerRotationY);
playerCapsule.WorldTransform = Matrix3X3.CreateFromQuaternion(playerCapsule.Orientation);
the problem is I can't use Matrix3x3 for worldTransform , what type of matrix that the worldTransform accept?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

If you assign the Orientation property, you do not need to set the WorldTransform property. In fact, doing so will overwrite the Position property because the WorldTransform contains both position and orientation.

The WorldTransform uses the XNA "Matrix" type because it's usually read to transform graphics, which rely mostly on the Matrix type. You can tell by mousing over the property and looking at the intellisense (and by the error, but it's nice to know before hitting a compiler error :)).
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

ok Norbo I need when player rotating the Z axis rotate with him (the z axis fornt the player always)
when I put the matrix.world I should write "matrix.quaternion(rotate)*matrix.translate(position)" that in normal situation but in bepu physics how it's work if I don't put the orientation * translation ?? how can I implement that
this is my translation code :
playerCapsule.LinearVelocity += new Vector3(-padState.ThumbSticks.Left.X * 10f, -10, padState.ThumbSticks.Left.Y * 10f);
and here is my rotation code:
playerCapsule.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Up, playerRotationY);

thanx :shock:
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

I would again recommend not rotating the actual player capsule. It's simplest to manage the aiming and turning using something in the game logic. The character's physics object does not need to turn. The SimpleCharacterControllerInput shows an example of this.
but in bepu physics how it's work if I don't put the orientation * translation ?? how can I implement that
You can still do the same thing, but it's often simpler just to supply the orientation to the Orientation property, and the position to the Position property. The WorldTransform property will reflect the changes made to those other properties, because the WorldTransform is a convenience wrapper around those other properties.

Note that changing the Position/Orientation directly amounts to teleporting the object. If teleportation is used as the primary form of movement, collisions cannot work, because the objects just teleport through each other. It's best to control objects with velocities as much as possible (I don't see you teleporting the position of the capsule, so it should be okay, but this is just for general reference).
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

I do that because the playerModel should follow the playerCapsule .... you told me that before..
now U told me to rotate the playerModel without rotate the playerCapsule is that right ?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

Yup. It's generally easiest to position the character using the physical capsule's position, and control the graphical/gameplay orientation using nonphysical gameplay logic. If you think about it intuitively, it makes some sense- the character's orientation is locked and its orientation is not the thing being simulated, while the position is something you are interested in simulating and reading.

It would be different if, for example, the character was a vehicle where the orientation is physical and simulated.
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

ok Norbo can we continue ?
see the image below :
bepu3.jpg
bepu3.jpg (208.51 KiB) Viewed 9079 times
I want the player to be the Z axis is forward when he turn right or left ! what going on with me like the left photo , what I want is how can the Bepu cordinate turn when the PlayerModel is rotate on Y axis .
I was see the "CharacterControllerConvexCastInput.cs " but it's diffrent I think from what I want .
Ok now see this code:
playerCapsule.LinearVelocity += new Vector3(0, -10, padState.ThumbSticks.Left.Y * 10f);
this code is just to translate the capsule around without rotate the coordinate.
and this code:
playerRotationY -= padState.ThumbSticks.Right.X * MathHelper.ToRadians(2f);
to increase or dec.. the amount of rotating using the Thumbsick.
and this:
playerQuaternion = Quaternion.CreateFromAxisAngle(Vector3.Up, playerRotationY);
this is for rotating the Model it self not the capsule.
and this code for attach the model to the capsule position:
playerPosition = playerCapsule.Position - new Vector3(0, 50, 0);
and this for PlayerModel.world:
playerAnimator.World =Matrix.CreateScale(0.5f)*Matrix.CreateFromQuaternion(playerQuaternion) *Matrix.CreateTranslation(playerPosition);
and now where or how can the Capsule coordinate rotate on Y ?
I don't know how it work exactly. please point me at right direction Norbo :shock:
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

I don't find the "SimpleCharacterControllerInput " in the source , is the "CharacterControllerInputOld.cs" the same ?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

The SimpleCharacterControllerInput can be found in BEPUphysicsDemos\BEPUphysicsDemos\Alternate Movement. The CharacterControllerOld and convex-cast based characters are different and more complicated.
I want the player to be the Z axis is forward when he turn right or left !
I understand that :) However, because the actual physics capsule's orientation is not being simulated, using it for the gameplay orientation of the character is not ideal. There are certain situations where it can actually cause problems because the orientation is getting nonphysically 'teleported.'

Because the orientation is driven entirely by game logic, and not physics, it is best to handle orientation entirely in game logic. Setting the character entity's orientation in this context amounts to using the entity's property as a storage device. This is bad because it's not just a storage device; there are physical consequences to setting that property. To avoid those consequences, use a suitable storage device, like a separate game logic orientation that you manage directly.
and now where or how can the Capsule coordinate rotate on Y ?
You've done all the hard work already. You have a gameplay driven orientation separate from the player's orientation (playerQuaternion), and you are apparently already using that orientation to draw the graphics. Now, just don't set the capsule's orientation to anything. In any piece of code where you referred to the physical capsule's orientation, simply refer to the separate gameplay orientation you made.
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

do you mean like that:
playerCapsule.Orientation = playerQuaternion;
:?:
if you want to end my pain please write the line that can finish my problem :wink:
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

That would set the capsule's orientation to the game play orientation you computed. That is something that you should not do.

There's no single line of code that does what I'm talking about. In every case where you refer to the capsule's orientation, just refer to the playerQuaternion you computed separately instead. Never refer to the character capsule's physical orientation.
alfarza
Posts: 45
Joined: Mon Jul 04, 2011 5:35 am

Re: Capsule movemet Matrix

Post by alfarza »

Ok norbo can those line help me (charcterControllerInputOld):

Vector2 totalMovement = Vector2.Zero;
Vector3 forward = Camera.WorldMatrix.Forward;
forward.Y = 0;
forward.Normalize();
Vector3 right = Camera.WorldMatrix.Right;
totalMovement += gamePadInput.ThumbSticks.Left.Y * new Vector2(forward.X, forward.Z);
totalMovement += gamePadInput.ThumbSticks.Left.X * new Vector2(right.X, right.Z);
CharacterController.MovementDirection = totalMovement;
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Capsule movemet Matrix

Post by Norbo »

Those lines show how to use camera's world matrix to pick the right movement direction.

You can use a similar approach by using your playerQuaternion. To get access to the Forward/Right properties, you'll need to create a Matrix from the quaternion first (or transform Vector3.Forward/Vector3.Right by the quaternion).

Also the value given to the MovementDirection field should be normalized; that's an oversight in the original. The gamepad input semi-normalizes it, but it's sometimes a bit off.
Post Reply