class Door : GameObjects
{
private Matrix[] DoorMatrix;
public bool dIsActive { get; set; }
public float time { get; set; }
private string DoorDirection;
private ModelBone DoorBone;
private ModelBone HandleDoorBone;
private Matrix mDoorTransform;
private Matrix mDoorHandleTransform;
private Vector3 PivotOfDoor;
private Vector3 PivotDoorHandle;
float Rotation;
float hRotation;
float angle;
float temp1 = 0f;
float temp2 = 0f;
//to doors
Matrix mDoorRotation;
Matrix PivotToOrigin;
Matrix OriginToPivot;
Matrix DoorRotationAboutPivot;
//to handle door
Matrix PivotToOriginHandle;
Matrix mDoorHandleRotation;
Matrix OriginToPivotHandle;
Matrix DoorHandleRotationAboutPivot;
}
In the Load method of the class called in the class constructor:
mDoorRotation = Matrix.Identity;
PivotToOrigin = Matrix.Identity;
OriginToPivot = Matrix.Identity;
DoorRotationAboutPivot = Matrix.Identity;
PivotToOriginHandle = Matrix.Identity;
mDoorHandleRotation = Matrix.Identity;
OriginToPivotHandle = Matrix.Identity;
DoorHandleRotationAboutPivot = Matrix.Identity;
ModelName = Content.Load<Model>(NameModel);
DoorMatrix = new Matrix[ModelName.Bones.Count];
DoorBone = ModelName.Bones[Bone01];
HandleDoorBone = ModelName.Bones[Bone02];
mDoorTransform = DoorBone.Transform;
mDoorHandleTransform = HandleDoorBone.Transform;
DoorDirection = "R";
PivotOfDoor = new Vector3(-13.065f, -1.208f, 1.119f)
PivotDoorHandle = Vector3.Zero;
Rotation = 120f;
hRotation = 50f;
angle = 120f
In the Update method of the class Door:
public void Update()
{
MoveHandleDoor();
OpenDoor();
}
The method MoveHandleDoor:
private void MoveHandleDoor()
{
if (dIsActive)
{
PivotToOriginHandle = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(-PivotDoorHandle);
this.mDoorHandleRotation = Matrix.CreateRotationZ(
MathHelper.Clamp(hRotation -= 0.1f * time,
MathHelper.ToRadians(-50f), MathHelper.ToRadians(0f)));
this.OriginToPivotHandle = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(PivotDoorHandle);
DoorHandleRotationAboutPivot =
PivotToOriginHandle * mDoorHandleRotation * OriginToPivotHandle;
HandleDoorBone.Transform = DoorHandleRotationAboutPivot * mDoorHandleTransform;
}
}
The method opendoor:
private void OpenDoor()
{
if (dIsActive)
{
PivotToOrigin = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(-PivotOfDoor);
if (DoorDirection == "L" || DoorDirection == "l")
{
mDoorRotation = Matrix.CreateRotationZ(
temp1 = MathHelper.Clamp(Rotation -= 0.001f * time,
MathHelper.ToRadians(-angle), MathHelper.ToRadians(0f)));
if( temp1 == MathHelper.ToRadians(-angle))
ReverseHandleDoor();
sounds.PlaySoundDoor("MetalDoor");
}
if (DoorDirection == "R" || DoorDirection == "r")
{
mDoorRotation = Matrix.CreateRotationZ(
temp2 = MathHelper.Clamp(Rotation += 0.001f * time,
MathHelper.ToRadians(0f), MathHelper.ToRadians(angle)));
if (temp2 == MathHelper.ToRadians(angle))
ReverseHandleDoor();
sounds.PlaySoundDoor("MetalDoor");
}
OriginToPivot = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(PivotOfDoor);
DoorRotationAboutPivot = PivotToOrigin * mDoorRotation * OriginToPivot;
DoorBone.Transform = mDoorTransform * DoorRotationAboutPivot;
}
sounds.UpdateSounds();
}
And the method is ReverseHandleDoor:
private void ReverseHandleDoor()
{
if (dIsActive)
{
PivotToOriginHandle = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(-PivotDoorHandle);
this.mDoorHandleRotation = Matrix.CreateRotationZ(
MathHelper.Clamp(hRotation += 0.1f * time ,
MathHelper.ToRadians(0f), MathHelper.ToRadians(50f)));
this.OriginToPivotHandle = Matrix.CreateScale(ModelScale) *
Matrix.CreateTranslation(PivotDoorHandle);
DoorHandleRotationAboutPivot =
PivotToOriginHandle * mDoorHandleRotation * OriginToPivotHandle;
HandleDoorBone.Transform = DoorHandleRotationAboutPivot * mDoorHandleTransform;
}
}
In the Update method is in Game1.cs
/ / The correct speed for the sound to be synchronized with the
/ / Motion model
door01.time = (float) gameTime.ElapsedGameTime.TotalMilliseconds / 2f;
/ / Triggers the movement of the door model
/ / This is just a test
if (Keyboard.GetState (). IsKeyDown (Keys.V))
door00.dIsActive = true;
door00.Update ();
Everything works fine in Debug mode, but in Release Mode the method ReverseHandleDoor is not called, then the door knob only turns to one side but does not return the right direction.
Could anyone tell me why this happens? The method works in Debug mode and crash in Release mode?
Thanks.