• Create Account

# tom_mai78101

Member Since 09 Jan 2007
Offline Last Active Yesterday, 02:08 PM

### In Topic: How To Obtain 3D Object Picking Up Distance Target Position From Camera View...

11 August 2016 - 12:06 PM

So instead of taking the ahead position, multiplying with the inverse of the view matrix, then using it in the model matrix, we do it in a slightly different order: Take the ahead position, plug it into the model matrix and then multiply with the inverse of the view matrix.

I finally understood what you mean by "plugging in". This means "translate" the model matrix by the aheadPosition. It took a while to figure out.

The object is now placed in front of the camera. Thanks again for your help!

### In Topic: How To Obtain 3D Object Picking Up Distance Target Position From Camera View...

11 August 2016 - 10:46 AM

Take the ahead position, plug it into the model matrix and then multiply with the inverse of the view matrix.

How do you "plug" it in? As in, out of the 4 rows/columns, what matrix row/column do I put the aheadPosition vector in?

### In Topic: How To Obtain 3D Object Picking Up Distance Target Position From Camera View...

11 August 2016 - 05:52 AM

*Duplicate post from above. Sorry.*

### In Topic: How To Obtain 3D Object Picking Up Distance Target Position From Camera View...

11 August 2016 - 05:52 AM

I simplified the code down to the very basic:

```//Main game engine rendering function pipeline in the codebase.
void Core::SceneRender(float interOcularDistance){
//Declaring reusable model matrix.
C3D_Mtx modelMatrix;

//Compute projection matrix and update matrix to shader program.
Mtx_PerspStereoTilt(&this->projectionMatrix, 40.0f * (std::acos(-1) / 180.0f), 400.0f / 240.0f, 0.01f, 1000.0f, interOcularDistance, 2.0f, false);

this->player.RenderUpdate(&this->viewMatrix);

//Compute view matrix and update matrix to shader program.

//Draw the vertex buffer objects.
for (size_t i = 0; i < this->gameObjects.size(); i++) {
//Switch buffers
this->gameObjects[i]->ConfigureBuffer();

//Calculate model view matrix.
Mtx_Identity(&modelMatrix);

if (this->player.cameraManipulateFlag){
C3D_Mtx inverse;
Mtx_Copy(&inverse, &this->viewMatrix);
Mtx_Inverse(&inverse);

//Doing the simplified calculations.
C3D_FVec aheadPosition = FVec4_New(0.0f, 0.0f, -3.0f, 1.0f);

text(18, 0, "Player picking up object.     ");
std::cout << "Ahead : " << std::fixed << std::setprecision(1) << aheadPosition.x << "  " << aheadPosition.y << "  " << aheadPosition.z << "  " << aheadPosition.w << std::endl;
std::cout << "Player: " << std::fixed << std::setprecision(1) << this->player.camX << "  0.0  " << this->player.camZ << std::endl;
std::cout << "Model Matrix: " << std::endl;
for (int i = 0; i < 16; i++){
float a = modelMatrix.m[(i/4) + (3-i%4)];
if (a < 0.0f){
std::cout << std::fixed << std::setprecision(1) << a << "  ";
}
else {
std::cout << std::fixed << std::setprecision(1) << a << "   ";
}
if (i % 4 == 3){
std::cout << std::endl;
}
}
}
else{
//This is to clear the output console.
text(18, 0, "Player not picking up object.");
text(19, 0, "                              ");
text(20, 0, "                              ");
text(21, 0, "                              ");
text(22, 0, "                              ");
text(23, 0, "                              ");
text(24, 0, "                              ");
text(25, 0, "                              ");
text(26, 0, "                              ");
text(27, 0, "                              ");
text(28, 0, "                              ");
}

//Render entity.
this->gameObjects[i]->Render();
}
}
```
And here's the animation of the model moving around:

Observing this, I can now see the object set at the world origin, while the camera is rotating around. When the player activates "cameraManipulationFlag", the object is picked up, and is placed directly in front of the player's camera. There's no rotations involved, so the object never faces the player.

From here, I can now see the object's previous rotation attempt is what's causing the glitchy translation issues. That also means I may need to re-figure out how to get the object oriented towards the player as the player "holds" the object around.

Thanks, AndorPatho, at least the positioning/translation works now. (Just not apparent in the GIF, since I have the output console showing it clearly).

I'm going to look into testing to see if the 3D graphics library contains Quaternion.LookAt() and see if it's possible to make the object always orient itself to the player.

### In Topic: How To Obtain 3D Object Picking Up Distance Target Position From Camera View...

11 August 2016 - 03:18 AM

I'm very close to fruition, but it doesn't seem to be right at the moment. After multiplying by the inverse of the view matrix, the object is somehow placed directly behind the player's camera. As the player camera rotates on the screen, the object rotates along with it, thus the player will never see it.

The rotation of the object is applied as modelMatrix * oldViewMatrix * (currentViewMatrix ^ -1).

The translation of the object is applied as modelMatrix * (currentViewMatrix ^ -1).

In between, the modelMatrix is never reverted to identity matrix, nor pushed to the matrix stack/popped back from matrix stack.

```void Player::Manipulate(std::shared_ptr<GameObject> obj, C3D_Mtx& currentProjectionMatrix, C3D_Mtx& currentViewMatrix, C3D_Mtx& modelMatrix){
if (this->cameraManipulateFlag){
//Matrix and C3D_Mtx.
//C3D_Mtx requires all rows to be [WZYX], instead of the other way around (XYZW).
//This is due to how the GPU reads the matrix data.
//Everything else mathematically is the same.

//Rotation - Obtaining the inverse matrix.
C3D_Mtx inverse, result;
Mtx_Copy(&inverse, &currentViewMatrix);
Mtx_Inverse(&inverse);

//Rotation - Multiplying the inverse with other matrices to get the actual rotation.
Mtx_Multiply(&result, &modelMatrix, &this->oldViewMatrix);
Mtx_Multiply(&modelMatrix, &result, &inverse);

//Gamedev.net
C3D_FVec aheadPosition = FVec4_New(0.0f, 0.0f, 2.0f, 1.0f);

text(19, 0, " ");
std::cout << std::fixed << std::setprecision(2) << "Ahead : " << aheadPosition.x << "    " << aheadPosition.y << "    " << aheadPosition.z << std::endl;
std::cout << std::fixed << std::setprecision(2) << "Player: " << this->camX << "  0.0  " << this->camZ << std::endl;