# 3D After Rotation, Wrong Location of Height being Displayed

## Recommended Posts

I am trying to make the height from the ballPosition.x and ballPosition.z accurate after a rotation.  Right now, when checking the ballPosition's height after a rotation, it has the height values of the old location with the new rotation.  It tries to rise above the incline in the wrong position.  I have included four code snippets and  a video to demonstrate.

This is the update of ballPosition, it isn't taking into effect any rotation.

	//forward
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS)
{
cameraPos.z = cameraPos.z - gMoveBallForward;
ballPosition.z = ballPosition.z - gMoveBallForward;
}

This causes rotation:

	if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS)
{
gkeypress = 2;
}
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
{
gkeypress = 1;
}

This changes the camera and rotates the world.

			ViewMatrix = glm::lookAt(cameraPos, glm::vec3(0.0f, 0.0f, -82.0f), cameraUp);
//p or o pressed - rotation
if (gkeypress) {
if (gkeypress == 1)
{
gchange = gchange + .1005f;
}
else
{
gchange = gchange - .1005f;
}
gkeypress = 0;
}
ModelMatrix = glm::rotate(ModelMatrix, glm::radians(gchange), glm::vec3(0.0f, 1.0f, 0.0f));
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(-19.5f, 0.0f, -24.5f));
ProjectionMatrix = glm::perspective(glm::radians(30.f), (float)1366 / (float)768, .001f, 1000.0f);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;



This is faulty code, when there has been a rotate, it finds and displays the height of the ball at the wrong location.

			float heightvalue = getHeightOfTerrain(ballPosition.x, ballPosition.z);
ModelMatrix1 = glm::translate(ModelMatrix1, glm::vec3(0.0f, heightvalue-.8f, (-13.0f)));


Josheir

##### Share on other sites
Posted (edited)
9 hours ago, Josheir said:

This is faulty code, when there has been a rotate, it finds and displays the height of the ball at the wrong location.

﻿


float heightvalue = getHeightOfTerrain(ballPosition.x, ballPosition.z);
ModelMatrix1 = glm::translate(ModelMatrix1, glm::vec3(0.0f, heightvalue-.8f, (-13.0f)));


1

No, it doesn't. Its actually doing the right thing, since:

9 hours ago, Josheir said:

This is the update of ballPosition, it isn't taking into effect any rotation.


//forward
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS)
{
cameraPos.z = cameraPos.z - gMoveBallForward;
ballPosition.z = ballPosition.z - gMoveBallForward;
}
1

Your ball can only move along one axis, the z-axis and this is not affected by any rotations, as you said yourself. So the rotations have no effect on your "physical world", but on your camera. This means, that if you start moving the ball and you rotate the camera, the ball should not move in the view direction anymore. It's like circling around a moving car. The car is following a straight road while you are changing the view angle on that car. So why is the ball always keeping the same position in front of the camera? Probably because you messed up your render transformations. Additionally, what you can also observe if you have a sharp look during the rotation phase is, that the ball is not staying at the same point when you rotate.

The problem is the ModelMatrix in combination with your ViewMatrix:

ModelMatrix = glm::rotate(ModelMatrix, glm::radians(gchange), glm::vec3(0.0f, 1.0f, 0.0f));﻿
ModelMatrix = glm::translate(ModelMatrix, glm::vec3(-19.5f, 0.0f, -24.5f));
ProjectionMatrix = glm::perspective(glm::radians(30.f), (float)1366 / (float)768, .001f, 1000.0f);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

First of all, it seems to me, that there is a little bit of missing clarification, which matrix is supposed to do what. Generally, a matrix transforms an object from one space to another. That's why I don't like some naming like ModelMatrix. What does that do? Transform to model space? If yes, from which space? Transform from model space? To which space? I know that these terms are usually common practice, but they are not clear. Especially beginners get easily confused by this naming. A model matrix is usually a model-To-World matrix and places everything inside your world.

 What you want to do is this; Quote This changes the camera and rotates the world.﻿   ﻿ This needs a single world to camera transformation, usually done by a single ViewMatrix. So why is there still a "ModelMatrix"? Everything needs to be in world space at this point. A model(toWorld) matrix is an individual matrix for each object in your world and can't be part of a world-to-camera transformation! Even if it is just wrong naming, this should be a single transformation. I don't use GLM, but I think glm::lookAt should already provide your whole world-to-camera matrix. Read the docs for that.   I recommend you read a little bit more about coordinate transformations to get a better understanding of why and when you do which transformation. Try this tutorial and maybe some more: https://learnopengl.com/Getting-started/Transformations   Greetings
Edited by DerTroll

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 9
• 9