# Relative 2D rotation and Camera following?

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

## Recommended Posts

Hi!
I have a 2d texture drawing (my car) . Now i want this car to rotate left and right when the buttons are pushed.
Since this is a game where its birds eye view, rotation im guessing would only need to be on the X-axis . On top of that , it has to be relative to the players speed. If you imagine a car driving around holding forward and right would cause the car to sort of 'swerve'.
Also , how could i make my camera follow my player.x and player.y position?
Ive tried various ways and each seems to not work properly.

Car:
 #include <math.h> #include "Car.h" #include "MenuSystem.h" #pragma comment(lib, "d3dx10d.lib") extern MenuSystem gamemenu; extern Car carclass; // variables defined // constructor Car::Car() { playerX = 0; // player x pos playerY = 0; // player y pos playerZ = 0; playerSpeed = 0.0f; // players speed playerMaxSpeed= 0.5f; // players maximum speed playerRotation = 0.000000001f; // player rotation angle } void Car::PlayerMovement() { if ( gamemenu.startOn == true ) { if ( carclass.playerSpeed > carclass.playerMaxSpeed) { playerSpeed = 0.2f; } if (GetAsyncKeyState(VK_UP ) ) { playerSpeed += 0.001f * 0.0001f; playerY += playerSpeed; } if (GetAsyncKeyState(VK_DOWN ) ) { playerY -= 0.001f; } if (GetAsyncKeyState(VK_RIGHT ) ) { //playerX -= 0.001f; playerY -= playerY - sin(playerRotation); } if (GetAsyncKeyState(VK_RIGHT ) ) { playerY -= 0.001f; } } } //void Car::Rotation() //{ // playerX = playerX + cos(playerRotation); // playerY = playerY - sin(playerRotation); // // }

Rendering:
 if ( gamemenu.startOn == true) { //set resources for plane 2 (PLAYER) D3DXMatrixTranslation( &worldMatrix, carclass.playerX, carclass.playerY,2.0 ); worldMatrix = scalingPlayer * worldMatrix ; pWorldMatrixEffectVariable->SetMatrix(worldMatrix); pTextureSR->SetResource( pPlayerTex ); //apply technique pBasicTechnique->GetPassByIndex( p )->Apply( 0 ); pCubeMesh->DrawSubset(0); 

Camera:
// Set up the view and projection matrices //***************************************************************************** const D3DXVECTOR3 camera[] = { D3DXVECTOR3(0.0f, 0.0f, 0.0f), D3DXVECTOR3(0.0f, 0.0f, 1.0f), D3DXVECTOR3(0.0f, 1.0f, 0.0f)}; D3DXMatrixLookAtLH(&viewMatrix, &camera[0], &camera[1], &camera[2] ); D3DXMatrixPerspectiveFovLH(&projectionMatrix, (float)D3DX_PI * 0.5f, (float)width/(float)height, 0.1f, 20.0f); D3DX10CreateFont(pD3DDevice, 32, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &mFont); //set shader matrices pViewMatrixEffectVariable->SetMatrix(viewMatrix); pProjectionMatrixEffectVariable->SetMatrix(projectionMatrix);

Anybody?

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

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

• Total Topics
633725
• Total Posts
3013567
×