Jump to content

  • Log In with Google      Sign In   
  • Create Account


Error only in Release Mode


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 SnakeBr   Members   -  Reputation: 101

Like
0Likes
Like

Posted 04 March 2011 - 06:29 PM

Since I´m a novice programmer does not understand why this error happens, I have a class that represents a port, the attributes of the class are

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.

Sponsor:

#2 Burnt_Fyr   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 04 March 2011 - 08:12 PM

Since I´m a novice programmer does not understand why this error happens, I have a class that represents a port, the attributes of the class are

...


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.


Does the program "crash" or just not function the way it is intended? In the debug build, your IDE will fill in default values(0) for everything, where as in release it's all up to you. Most likely you are using something that you haven't initialized yourself, and because of that, it will be filled with garbage.

#3 SnakeBr   Members   -  Reputation: 101

Like
0Likes
Like

Posted 04 March 2011 - 10:42 PM

The program does not crash. What happens is that in debug mode it behaves as expected, as is the video (please see full screen, the program I'm using always makes the image darker and no good I set the lights otherwise):



But when the Release mode is selected, the door handle does not return to normal position. Watch the video:



I've got the project settings and when I deselect the text box "Define DEBUG constant". What I do not understand is that when I deselect the "Define DEBUG constant" (even though we are in Debug mode) which is the same mode setting release the door handle back into position. I do not think that is a problem to pick up garbage from memory, all Matrix used are cleared so that the class is called in the constructor.

#4 Adam_42   Crossbones+   -  Reputation: 2361

Like
1Likes
Like

Posted 05 March 2011 - 04:06 AM

What's almost certainly going wrong here is that you're having floating point precision issues in this bit of code:

if( temp1 == MathHelper.ToRadians(-angle))
  ReverseHandleDoor();

You should never test floats for exact equality. You generally want a test that looks something like: if (abs(b-a) < EPSILON)

For much more detail on how floating point numbers work take a look at http://www.cse.msu.edu/~cse320/Documents/FloatingPoint.pdf

#5 SnakeBr   Members   -  Reputation: 101

Like
0Likes
Like

Posted 05 March 2011 - 10:55 PM

What's almost certainly going wrong here is that you're having floating point precision issues in this bit of code:

if( temp1 == MathHelper.ToRadians(-angle))
  ReverseHandleDoor();

You should never test floats for exact equality. You generally want a test that looks something like: if (abs(b-a) < EPSILON)

For much more detail on how floating point numbers work take a look at http://www.cse.msu.e...oatingPoint.pdf


Adam_42 thanks for the help, really you're right. The problem was related to math with float, so now the program in Debug mode when in Release mode is working as expected.
Here's the thing I did differently according to the. pdf you posted.

if (Math.Abs(temp1 - MathHelper.ToRadians(-angle)) < 0.00001f)
     			ReverseHandleDoor();

Thank you also for helping Burnt_Fyr.

#6 Burnt_Fyr   Members   -  Reputation: 1200

Like
0Likes
Like

Posted 15 March 2011 - 11:32 AM

Thanks for the thanks... and here's a tip, rather than using a hard coded value, create a const int or #define macro to hold the value you use for epsilon, just in case you ever need to change it



const int EPSILON = 0.00001f

//...

if (Math.Abs(temp1 - MathHelper.ToRadians(-angle)) < EPSILON )
                        ReverseHandleDoor();


#7 marius1930   Members   -  Reputation: 119

Like
0Likes
Like

Posted 15 March 2011 - 03:17 PM

There's always FLT_EPSILON / numeric_limits::epsilon




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS