Jump to content
  • Advertisement
Sign in to follow this  
ryan mccabe

simple problem with assigning a value

This topic is 2352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some really simple code that does not do what it should. The function gets an x and a y value, updates if there is a change, or sets a value to 0 if no change has occurred. The problem is that the value isn't being set to 0.


bool CameraClass::Update(float x, float y)
{

// right mouse button for camera rotation
if(GetAsyncKeyState(0x02))
{
if(m_rotationX != x)
m_rotationX = x;
else {
//here is the problem. m_rotation is not being changed. The same happens with the y value.
m_rotationX = 0.0f;
printFloat(m_rotationX);
printFloat(x);
}
if(m_rotationY != y)
m_rotationY = y;
else m_rotationY = 0.0f;
}
if(!GetAsyncKeyState(0x02))
{
m_rotationX = 0;
m_rotationY = 0;
}
return true;
}


printFloat is only called if m_rotationX is set to 0 first, but in the print out the values always print the matching pair. So if m_rotationX is 100 and x is 100 i get (100, 100) printed. What should happen is I get (0, 100) since the pair matched and m_rotationX was set to 0.

Its boggling my mind a bit. I don't see how setting a value in such a straight forward way could fail. Any advice?

Edit: camera behavior also confirms the print.

Share this post


Link to post
Share on other sites
Advertisement
I just did a quick mock-up of the situation you're describing. If I'm understanding the problem you are having, it didn't occur in my test; the assignment worked. Have you tried walking through that spot in the debugger and watching the value of m_rotationX? Perhaps it is being changed and there is a problem with printFloat.

[source lang="c++"]
#include "stdafx.h"
#include <iostream>

using namespace std;

float m_rotationX;
float m_rotationY;

bool Update(float x, float y, bool RMB);


int _tmain(int argc, _TCHAR* argv[])
{
m_rotationX = 100;
m_rotationY = 100;

Update(100, 100, true);

return 0;
}

bool Update(float x, float y, bool RMB)
{

// right mouse button for camera rotation
if(RMB)
{
if(m_rotationX != x)
m_rotationX = x;
else
{
//This works fine
m_rotationX = 0.0f;
cout << m_rotationX << endl;
cout << x << endl;
}
if(m_rotationY != y)
m_rotationY = y;
else m_rotationY = 0.0f;
}
if(!RMB)
{
m_rotationX = 0;
m_rotationY = 0;
}
return true;
}
[/source]

Share this post


Link to post
Share on other sites
Just to be snotty, I'd also recommend breaking that method up as well. It would be easier to change and debug if you did something like this.
[source lang="c++"]
void CameraClass::Update(float x, float y, bool RMB)
{
if(RMB)
{
ChangeCamera( x, y );
}
else
{
ResetCamera( );
}
}

void CameraClass::ChangeCamera( float x, float y )
{
if(m_rotationX != x)
m_rotationX = x;
else
{
m_rotationX = 0.0f;
printFloat(m_rotationX);
printFloat(x);
}

m_rotationY = (m_rotationY != y) ? y : 0.0f;
}

void CameraClass::ResetCamera( )
{
m_rotationX = 0;
m_rotationY = 0;
}
[/source]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!