From d54cffee8e0c785daedb0bc6457f71e52db5a3b4 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Tue, 4 Feb 2014 11:50:15 +0100 Subject: [PATCH] GL - camera follows player --- .../DanBiasGame/GameClientState/GameState.cpp | 24 +++++++++---- .../DanBiasGame/GameClientState/GameState.h | 1 + Code/Game/GameLogic/Object.cpp | 34 +++++++++---------- Code/Game/GameLogic/Player.cpp | 24 +++++++------ 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index e57d004d..3e058619 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -43,7 +43,7 @@ bool GameState::Init(Oyster::Network::NetworkClient* nwClient) privData->state = gameStateState_loading; privData->nwClient = nwClient; privData->state = LoadGame(); - + pitch = 0; return true; } GameState::gameStateState GameState::LoadGame() @@ -309,7 +309,8 @@ void GameState::readKeyInput(InputClass* KeyInput) if (KeyInput->IsMousePressed()) { camera->Yaw(-KeyInput->GetYaw()); - camera->Pitch(KeyInput->GetPitch()); + //camera->Pitch(KeyInput->GetPitch()); + //pitch = KeyInput->GetPitch(); camera->UpdateViewMatrix(); GameLogic::Protocol_PlayerLook playerLookDir; Oyster::Math::Float4 look = camera->GetLook(); @@ -391,10 +392,21 @@ void GameState::Protocol( ObjPos* pos ) { camera->setRight((Oyster::Math::Float3(world[0], world[1], world[2]))); camera->setUp(Oyster::Math::Float3(world[4], world[5], world[6])); - //camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10]))); - Oyster::Math::Float3 pos = Oyster::Math::Float3(world[12], world[13]+2, world[14]+2); - //Oyster::Math::Float3 cameraPos = up + pos; - camera->SetPosition(pos); + Oyster::Math::Float3 cameraLook = camera->GetLook(); + Oyster::Math::Float3 objForward = (Oyster::Math::Float3(world[8], world[9], world[10])); + + camera->setLook(objForward); + camera->UpdateViewMatrix(); + Oyster::Math::Float3 pos = Oyster::Math::Float3(world[12], world[13], world[14]); + Oyster::Math::Float3 up = Oyster::Math::Float3(world[4], world[5], world[6]); + + up *= 2; + objForward *= -3; + Oyster::Math::Float3 cameraPos = up + pos + objForward; + //camera->Pitch(pitch); + camera->SetPosition(cameraPos); + //camera->LookAt(pos, dir, up); + //Oyster::Math::Float3 newLook = objForward; camera->UpdateViewMatrix(); } } diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.h b/Code/Game/DanBiasGame/GameClientState/GameState.h index 04d5b791..d555134c 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameState.h @@ -27,6 +27,7 @@ private: Camera* camera; int myId; + float pitch; struct myData; myData* privData; public: diff --git a/Code/Game/GameLogic/Object.cpp b/Code/Game/GameLogic/Object.cpp index b55ed661..a000eaed 100644 --- a/Code/Game/GameLogic/Object.cpp +++ b/Code/Game/GameLogic/Object.cpp @@ -118,24 +118,9 @@ Oyster::Physics::ICustomBody* Object::GetRigidBody() void Object::BeginFrame() { - //newPhysicsState.SetAngularMomentum(Float3::null); - //this->rigidBody->SetState(this->newPhysicsState); - //this->rigidBody->GetState(this->newPhysicsState); - Oyster::Math::Float4 axis; - if(newPhysicsState.GetGravityNormal()!= Float3::null) - { - Oyster::Math3D::SnapAngularAxis(Oyster::Math::Float4(newPhysicsState.GetAngularAxis(), 0), Oyster::Math::Float4::standard_unit_y, -Oyster::Math::Float4(newPhysicsState.GetGravityNormal()), axis); - if(axis !=axis) - { - //error - int i =0 ; - } - //newPhysicsState.SetRotation(axis.xyz); - newPhysicsState.SetAngularMomentum(Float3::null); - Oyster::Math::Float3 debug = ::LinearAlgebra3D::WorldAxisOf(::LinearAlgebra3D::Rotation(axis.xyz), Oyster::Math::Float3::standard_unit_y); - debug += newPhysicsState.GetGravityNormal(); - } + + this->rigidBody->SetState(this->newPhysicsState); @@ -144,6 +129,21 @@ void Object::BeginFrame() void Object::EndFrame() { this->currPhysicsState = this->rigidBody->GetState(); + + if(currPhysicsState.GetGravityNormal()!= Float3::null) + { + Oyster::Math::Float4 axis; + Oyster::Math3D::SnapAngularAxis(Oyster::Math::Float4(currPhysicsState.GetAngularAxis(), 0), Oyster::Math::Float4::standard_unit_y, -Oyster::Math::Float4(currPhysicsState.GetGravityNormal()), axis); + if(axis !=axis) + { + //error + int i =0 ; + } + currPhysicsState.SetRotation(axis.xyz); + currPhysicsState.SetAngularMomentum(Float3::null); + Oyster::Math::Float3 debug = ::LinearAlgebra3D::WorldAxisOf(::LinearAlgebra3D::Rotation(axis.xyz), Oyster::Math::Float3::standard_unit_y); + debug += currPhysicsState.GetGravityNormal(); + } this->newPhysicsState = this->currPhysicsState; } diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index f205a516..2ca1cc2d 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -98,21 +98,21 @@ void Player::Move(const PLAYER_MOVEMENT &movement) void Player::MoveForward() { - //Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; - Oyster::Math::Float3 forward = lookDir; + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + //Oyster::Math::Float3 forward = lookDir; newPhysicsState.ApplyLinearImpulse(forward * (30000 * this->gameInstance->GetFrameTime())); } void Player::MoveBackwards() { - //Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; - Oyster::Math::Float3 forward = lookDir; + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + //Oyster::Math::Float3 forward = lookDir; newPhysicsState.ApplyLinearImpulse(-forward * 30000 * this->gameInstance->GetFrameTime()); } void Player::MoveRight() { //Do cross product with forward vector and negative gravity vector - //Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; - Oyster::Math::Float3 forward = lookDir; + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + //Oyster::Math::Float3 forward = lookDir; Oyster::Math::Float3 r = (-currPhysicsState.GetGravityNormal()).Cross(forward); newPhysicsState.ApplyLinearImpulse(-r * 30000 * this->gameInstance->GetFrameTime()); @@ -120,8 +120,8 @@ void Player::MoveRight() void Player::MoveLeft() { //Do cross product with forward vector and negative gravity vector - //Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; - Oyster::Math::Float3 forward = lookDir; + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + //Oyster::Math::Float3 forward = lookDir; Oyster::Math::Float3 r = (-currPhysicsState.GetGravityNormal()).Cross(forward); //Still get zero newPhysicsState.ApplyLinearImpulse(r * 30000 * this->gameInstance->GetFrameTime()); } @@ -141,11 +141,15 @@ void Player::Respawn(Oyster::Math::Float3 spawnPoint) void Player::Rotate(const Oyster::Math3D::Float4 lookDir) { Oyster::Math::Float dx = lookDir.w; + if(dx > 0.0f) + { + int i =0 ; + } Oyster::Math::Float3 up = currPhysicsState.GetOrientation().v[1]; - Oyster::Math::Float3 deltaAxis = up * (-dx * 0.2) ; + Oyster::Math::Float3 deltaAxis = up * (-dx * 0.02) ; Oyster::Math::Float3 oldOrt = currPhysicsState.GetRotation(); - //newPhysicsState.SetRotation(oldOrt + deltaAxis); + newPhysicsState.SetRotation(oldOrt + deltaAxis); this->lookDir = lookDir.xyz; }