Public Group

# Strange Vector Problem

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

## Recommended Posts

Hiya,

I'm storing a position vector and a normalised direction vector for the player. In the Update() method, the direction is rotated by the y-axis to yaw. Then in the Render() method, I convert the direction to Euler angles to draw.

For some reason it's not working correctly. I've implemented debug line drawing, and I'm seeing the direction vector flip between +/- 90 degrees around y constantly.

Can anyone spot anything obvious?

I can verify it's working correctly up to and including calculating the required angle to yaw...

I also believe the axis rotations are correct.

 void PlayerObject::update(float elapsedSeconds) { /* Get the player mouse movement. Returns value in the range -1..1 */ float yawAmount = Mouse::instance().getNormalisedYaw(); float pitchAmount = Mouse::instance().getNormalisedPitch(); /* Calculate the angles to yaw and pitch this frame. */ float yawAngle = yawAmount * MAX_YAW_PER_SECOND * elapsedSeconds; float pitchAngle = pitchAmount * MAX_PITCH_PER_SECOND * elapsedSeconds; /* Clamp the pitch angle. */ static float totalPitch = 0.0f; totalPitch += pitchAngle; if(totalPitch > MAX_PITCH) { totalPitch = MAX_PITCH; pitchAngle = 0.0f; } else if(totalPitch < MIN_PITCH) { totalPitch = MIN_PITCH; pitchAngle = 0.0f; } /* The above works correctly. The problem must be below somewhere... */ static Vector3 localX(1.0f, 0.0f, 0.0f); static Vector3 localY(0.0f, 1.0f, 0.0f); static Vector3 localZ(0.0f, 0.0f, 1.0f); Matrix4 rotationMatrix; /* Yaw - Rotate the local x- and z-axis around the y-axis. */ if(yawAngle != 0.0f) { rotationMatrix.rotate(Vector3(0.0f, 1.0f, 0.0f), yawAngle); localX = localX * rotationMatrix; localZ = localZ * rotationMatrix; } /* Pitching snipped until I can get this working. */ /* Keep the axes orthogonal. */ localZ.normalise(); localY = localZ.cross(localX); localY.normalise(); localX = localY.cross(localZ); localX.normalise(); /* New direction is the opposite of the local z-axis. */ m_direction = localZ * -1.0f; } /**********************************************/ void PlayerObject::draw(RenderQueue& renderQueue) { /* Calculate the Euler angles for yaw and pitch from the direction vector. */ float yaw = atan2(m_direction.x, m_direction.y); float pitch = atan2(m_direction.z, sqrtf((m_direction.x * m_direction.x) + (m_direction.y * m_direction.y))); /* Convert to degrees. */ yaw *= 180.0f / 3.1415926f; pitch *= 180.0f / 3.1415926f; renderQueue.addToRenderQueue(m_mesh, m_position, yaw, /* pitch */ 0.0f); } 

I think the problem might be with the euler angle calculations in render(). When the yaw angle was > 0 it is always calculated as 90 degrees, if the angle was < 0 it is always calculated as -90 degrees...

Thanks very much for any help! I've been staring at this for too long

##### Share on other sites
Quick question, sorry but Im not sure why its not working, but if you need the angles in draw, and you have them in update, could you not just store them from update, and avoid recalculating them in draw?

##### Share on other sites

Quick question, sorry but Im not sure why its not working, but if you need the angles in draw, and you have them in update, could you not just store them from update, and avoid recalculating them in draw?

I'm keeping the direction vector to make the movement calculations easily...

Good point. I could just keep a running total of the yaw/pitch and use that instead.

##### Share on other sites
Hidden
Another thought, Im thinking the problem may come from the continuous rotating of the localx and localz. What if you tried tracking the total yaw similar to how the pitch is done, clamp it to 0 to 360, and use this total yaw to rotate the unit x (1.0, 0.0, 0.0) and z (0.0, 0.0, 1.0).

Edit: I see that is what you are actually doing, didnt see the static vectors.

##### Link to post
Success! It was the Euler calculations.. I found a post by Aph3x which had the correct equations.

The last problem I seem to be having is that the player rotates way too fast. If I replace MAX_YAW_PER_SECOND with its equivalent in radians the player yaws at the correct speed, but the angle is then not clamped right.

Can anyone see the problem?

##### Share on other sites
Pff.. I was converting degrees to radians in the matrix rotation method, then using the original degree angle for the calculations :=/

Smooth!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634086
• Total Posts
3015412
×