From a3886c4f1f688a5b95cafee0ccc829891d67fcf1 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Mon, 3 Feb 2014 15:52:00 +0100 Subject: [PATCH] GL - added models and started to fix camera movement --- .../DanBiasGame/GameClientState/GameState.cpp | 45 ++++++++++++++--- Code/Game/GameLogic/Game.h | 2 +- Code/Game/GameLogic/GameAPI.h | 2 +- Code/Game/GameLogic/Game_PlayerData.cpp | 9 +++- Code/Game/GameLogic/Object.cpp | 48 ++++++++++--------- Code/Game/GameLogic/Object.h | 7 +-- Code/Game/GameLogic/Player.cpp | 43 +++++++++++------ Code/Game/GameLogic/Player.h | 2 +- Code/Game/GameProtocols/PlayerProtocols.h | 6 +++ .../Implementation/GameSession_Gameplay.cpp | 3 +- 10 files changed, 115 insertions(+), 52 deletions(-) diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index cd281717..638d954d 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -107,7 +107,7 @@ bool GameState::LoadModels(std::wstring mapFile) modelData.world = modelData.world * translate; modelData.visible = true; - modelData.modelPath = L"..\\Content\\Models\\char_white.dan"; + modelData.modelPath = L"char_renderTest.dan"; modelData.id = 2; // load models obj = new C_Player(); @@ -116,17 +116,43 @@ bool GameState::LoadModels(std::wstring mapFile) // add player model 2 modelData.world = Oyster::Math3D::Float4x4::identity; - translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(10, 320, 0)); + translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(50, 320, 0)); modelData.world = modelData.world * translate; modelData.visible = true; - modelData.modelPath = L"..\\Content\\Models\\char_white.dan"; + modelData.modelPath = L"char_renderTest.dan"; modelData.id = 3; // load models obj = new C_Player(); privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); + // add house model + modelData.world = Oyster::Math3D::Float4x4::identity; + translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(50, 300, 0)); + //Oyster::Math3D::RotationMatrix_AxisZ() + modelData.world = modelData.world * translate; + modelData.visible = true; + modelData.modelPath = L"building_corporation.dan"; + modelData.id = 4; + // load models + obj = new C_Player(); + privData->object.push_back(obj); + privData->object[privData->object.size() -1 ]->Init(modelData); + + // add crystal model + modelData.world = Oyster::Math3D::Float4x4::identity; + translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(10, 305, 0)); + + modelData.world = modelData.world * translate; + modelData.visible = true; + modelData.modelPath = L"crystalformation_b.dan"; + modelData.id = 5; + // load models + obj = new C_Player(); + privData->object.push_back(obj); + privData->object[privData->object.size() -1 ]->Init(modelData); + return true; } @@ -282,14 +308,16 @@ void GameState::readKeyInput(InputClass* KeyInput) //send delta mouse movement if (KeyInput->IsMousePressed()) { - camera->Yaw(KeyInput->GetYaw()); + camera->Yaw(-KeyInput->GetYaw()); camera->Pitch(KeyInput->GetPitch()); camera->UpdateViewMatrix(); GameLogic::Protocol_PlayerLook playerLookDir; - Oyster::Math::Float3 look = camera->GetLook(); + Oyster::Math::Float4 look = camera->GetLook(); playerLookDir.lookDirX = look.x; playerLookDir.lookDirY = look.y; playerLookDir.lookDirZ = look.z; + playerLookDir.deltaX = -KeyInput->GetYaw(); + privData->nwClient->Send(playerLookDir); } @@ -357,11 +385,14 @@ void GameState::Protocol( ObjPos* pos ) { privData->object[i]->setPos(world); //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]))); if(i == myId) // playerobj { - camera->SetPosition(Oyster::Math::Float3(world[12], world[13]+2.2f, world[14]-1)); + Oyster::Math::Float3 up = (Oyster::Math::Float3(world[4], world[5]+2, world[6])); + Oyster::Math::Float3 pos = Oyster::Math::Float3(world[12], world[13]+2, world[14]); + Oyster::Math::Float3 cameraPos = up + pos; + camera->SetPosition(pos); camera->UpdateViewMatrix(); } } diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h index 3ea76b61..55f3e4cf 100644 --- a/Code/Game/GameLogic/Game.h +++ b/Code/Game/GameLogic/Game.h @@ -39,7 +39,7 @@ namespace GameLogic Oyster::Math::Float4x4 GetOrientation() override; int GetID() const override; OBJECT_TYPE GetObjectType() const override; - void Rotate(const Oyster::Math3D::Float3 lookDir) override; + void Rotate(const Oyster::Math3D::Float4 lookDir) override; Player *player; }; diff --git a/Code/Game/GameLogic/GameAPI.h b/Code/Game/GameLogic/GameAPI.h index ee4bad83..f0ce50b1 100644 --- a/Code/Game/GameLogic/GameAPI.h +++ b/Code/Game/GameLogic/GameAPI.h @@ -79,7 +79,7 @@ namespace GameLogic * @param x: The relative x axis * @param y: The relative y axis **/ - virtual void Rotate(const Oyster::Math3D::Float3 lookDir) = 0; + virtual void Rotate(const Oyster::Math3D::Float4 lookDir) = 0; /******************************************************** * Uses the chosen players weapon based on input diff --git a/Code/Game/GameLogic/Game_PlayerData.cpp b/Code/Game/GameLogic/Game_PlayerData.cpp index e19af2f3..18d058d0 100644 --- a/Code/Game/GameLogic/Game_PlayerData.cpp +++ b/Code/Game/GameLogic/Game_PlayerData.cpp @@ -11,13 +11,18 @@ Game::PlayerData::PlayerData() sbDesc.size = Oyster::Math::Float3(4,7,4); sbDesc.mass = 70; sbDesc.restitutionCoeff = 0.5; + sbDesc.rotation = Oyster::Math3D::RotationMatrix_AxisY(Utility::Value::Radian(180)); //create rigid body Oyster::Physics::ICustomBody *rigidBody = Oyster::Physics::API::Instance().CreateRigidBody(sbDesc).Release(); //create player with this rigid body this->player = new Player(rigidBody,Object::DefaultCollisionBefore, Player::PlayerCollision, OBJECT_TYPE::OBJECT_TYPE_PLAYER); this->player->GetRigidBody()->SetCustomTag(this); - + Oyster::Physics::ICustomBody::State state; + this->player->GetRigidBody()->GetState(state); + state.SetRotation(Oyster::Math::Float3(0, Oyster::Math::pi, 0)); + this->player->GetRigidBody()->SetState(state); + player->EndFrame(); } Game::PlayerData::PlayerData(int playerID,int teamID) { @@ -61,7 +66,7 @@ OBJECT_TYPE Game::PlayerData::GetObjectType() const { return this->player->GetObjectType(); } -void Game::PlayerData::Rotate(const Oyster::Math3D::Float3 lookDir) +void Game::PlayerData::Rotate(const Oyster::Math3D::Float4 lookDir) { this->player->Rotate(lookDir); } \ No newline at end of file diff --git a/Code/Game/GameLogic/Object.cpp b/Code/Game/GameLogic/Object.cpp index a0823247..8692aa72 100644 --- a/Code/Game/GameLogic/Object.cpp +++ b/Code/Game/GameLogic/Object.cpp @@ -22,8 +22,8 @@ Object::Object() this->type = OBJECT_TYPE::OBJECT_TYPE_UNKNOWN; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } Object::Object(OBJECT_TYPE type) @@ -34,8 +34,8 @@ Object::Object(OBJECT_TYPE type) Oyster::Physics::API::Instance().AddObject(rigidBody); this->type = type; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } Object::Object(Oyster::Physics::ICustomBody *rigidBody, OBJECT_TYPE type) @@ -44,8 +44,8 @@ Object::Object(Oyster::Physics::ICustomBody *rigidBody, OBJECT_TYPE type) this->rigidBody = rigidBody; this->type = type; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } Object::Object(void* collisionFuncBefore, void* collisionFuncAfter, OBJECT_TYPE type) @@ -57,8 +57,8 @@ Object::Object(void* collisionFuncBefore, void* collisionFuncAfter, OBJECT_TYPE this->type = type; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } Object::Object(Oyster::Physics::ICustomBody *rigidBody ,void* collisionFuncBefore, void* collisionFuncAfter, OBJECT_TYPE type) @@ -71,8 +71,8 @@ Object::Object(Oyster::Physics::ICustomBody *rigidBody ,void* collisionFuncBefor this->type = type; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } Object::Object(Oyster::Physics::ICustomBody *rigidBody ,Oyster::Physics::ICustomBody::SubscriptMessage (*collisionFuncBefore)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter), Oyster::Physics::ICustomBody::SubscriptMessage (*collisionFuncAfter)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter,Oyster::Math::Float kineticEnergyLoss), OBJECT_TYPE type) @@ -86,13 +86,13 @@ Object::Object(Oyster::Physics::ICustomBody *rigidBody ,Oyster::Physics::ICustom this->type = type; this->objectID = GID(); - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } void Object::ApplyLinearImpulse(Oyster::Math::Float3 force) { - setState.ApplyLinearImpulse(force); + newPhysicsState.ApplyLinearImpulse(force); } @@ -118,24 +118,28 @@ Oyster::Physics::ICustomBody* Object::GetRigidBody() void Object::BeginFrame() { + + this->rigidBody->SetState(this->newPhysicsState); + this->rigidBody->GetState(this->newPhysicsState); + Oyster::Math::Float4 axis; - if(setState.GetGravityNormal()!= Float3::null) + if(newPhysicsState.GetGravityNormal()!= Float3::null) { - Oyster::Math3D::SnapAngularAxis(Oyster::Math::Float4(setState.GetAngularAxis(), 0), Oyster::Math::Float4::standard_unit_y, -Oyster::Math::Float4(setState.GetGravityNormal()), axis); - setState.SetRotation(axis.xyz); - setState.SetAngularMomentum(Float3::null); + Oyster::Math3D::SnapAngularAxis(Oyster::Math::Float4(newPhysicsState.GetAngularAxis(), 0), Oyster::Math::Float4::standard_unit_y, -Oyster::Math::Float4(newPhysicsState.GetGravityNormal()), axis); + 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 += setState.GetGravityNormal(); + debug += newPhysicsState.GetGravityNormal(); } + - - this->rigidBody->SetState(this->setState); + this->rigidBody->SetState(this->newPhysicsState); } // update physic void Object::EndFrame() { - this->getState = this->rigidBody->GetState(); - this->setState = this->getState; + this->currPhysicsState = this->rigidBody->GetState(); + this->newPhysicsState = this->currPhysicsState; } void Object::setBeforeCollisonFunc(Oyster::Physics::ICustomBody::SubscriptMessage (*collisionFuncBefore)(Oyster::Physics::ICustomBody *proto,Oyster::Physics::ICustomBody *deuter)) diff --git a/Code/Game/GameLogic/Object.h b/Code/Game/GameLogic/Object.h index d3a3690f..07437661 100644 --- a/Code/Game/GameLogic/Object.h +++ b/Code/Game/GameLogic/Object.h @@ -49,11 +49,12 @@ namespace GameLogic protected: Oyster::Physics::ICustomBody *rigidBody; - Oyster::Physics::ICustomBody::State setState; - Oyster::Physics::ICustomBody::State getState; + Oyster::Physics::ICustomBody::State newPhysicsState; + Oyster::Physics::ICustomBody::State currPhysicsState; static const Game* gameInstance; - + Oyster::Math::Float3 currLook; + Oyster::Math::Float3 newLook; }; } diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index ea36b284..1ab58d11 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -86,24 +86,28 @@ void Player::Move(const PLAYER_MOVEMENT &movement) void Player::MoveForward() { - setState.ApplyLinearImpulse(this->lookDir * (2000 * this->gameInstance->GetFrameTime())); + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + newPhysicsState.ApplyLinearImpulse(forward * (2000 * this->gameInstance->GetFrameTime())); } void Player::MoveBackwards() { - setState.ApplyLinearImpulse(-this->lookDir * 2000 * this->gameInstance->GetFrameTime()); + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + newPhysicsState.ApplyLinearImpulse(-forward * 2000 * this->gameInstance->GetFrameTime()); } void Player::MoveRight() { //Do cross product with forward vector and negative gravity vector - Oyster::Math::Float3 r = (-rigidBody->GetGravityNormal()).Cross((Oyster::Math::Float3)this->lookDir); - setState.ApplyLinearImpulse(r * 2000 * this->gameInstance->GetFrameTime()); + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + Oyster::Math::Float3 r = (-currPhysicsState.GetGravityNormal()).Cross(forward); + newPhysicsState.ApplyLinearImpulse(r * 2000 * this->gameInstance->GetFrameTime()); } void Player::MoveLeft() { //Do cross product with forward vector and negative gravity vector - Oyster::Math::Float3 r = -(-rigidBody->GetGravityNormal()).Cross((Oyster::Math::Float3)this->lookDir); //Still get zero - setState.ApplyLinearImpulse(-r * 2000 * this->gameInstance->GetFrameTime()); + Oyster::Math::Float3 forward = currPhysicsState.GetOrientation().v[2]; + Oyster::Math::Float3 r = (-currPhysicsState.GetGravityNormal()).Cross(forward); //Still get zero + newPhysicsState.ApplyLinearImpulse(-r * 2000 * this->gameInstance->GetFrameTime()); } void Player::UseWeapon(const WEAPON_FIRE &usage) @@ -119,20 +123,31 @@ void Player::Respawn(Oyster::Math::Float3 spawnPoint) this->lookDir = Oyster::Math::Float4(1,0,0); } -void Player::Rotate(const Oyster::Math3D::Float3 lookDir) +void Player::Rotate(const Oyster::Math3D::Float4 lookDir) { - this->lookDir = lookDir; - - Oyster::Math::Float4 up = -setState.GetGravityNormal(); - Oyster::Math::Float4 pos = setState.GetCenterPosition(); - Oyster::Math::Float4x4 world = Oyster::Math3D::OrientationMatrix_LookAtDirection(lookDir, up.xyz, pos.xyz); + Oyster::Math::Float dx = lookDir.w; + //deltaLook = this->lookDir - lookDir; + + //Oyster::Math::Float4 up = -setState.GetGravityNormal(); + //Oyster::Math::Float4 pos = setState.GetCenterPosition(); + //Oyster::Math::Float4x4 world = Oyster::Math3D::OrientationMatrix_LookAtDirection(lookDir, up.xyz, pos.xyz); + // cant set rotation + //setState.AddRotation(); + + Oyster::Math::Float3 up = currPhysicsState.GetOrientation().v[1]; + Oyster::Math::Float3 deltaAxis = up * (-dx * 0.02) ; + //setState.SetOrientation(world); + newPhysicsState.AddRotation(deltaAxis); + //setState.SetOrientation() //this->lookDir = lookDir - up.xyz; //this->lookDir = lookDir; //this->setState.AddRotation(Oyster::Math::Float4(x, y)); //this->setState.SetRotation(); + + this->lookDir = lookDir.xyz; } void Player::Jump() @@ -155,11 +170,11 @@ bool Player::IsIdle() Oyster::Math::Float3 Player::GetPosition() const { - return (Oyster::Math::Float3)getState.GetCenterPosition(); + return (Oyster::Math::Float3)currPhysicsState.GetCenterPosition(); } Oyster::Math::Float4x4 Player::GetOrientation() const { - return this->getState.GetOrientation(); + return this->currPhysicsState.GetOrientation(); } Oyster::Math::Float3 Player::GetLookDir() const { diff --git a/Code/Game/GameLogic/Player.h b/Code/Game/GameLogic/Player.h index 0df0d040..0dcb6043 100644 --- a/Code/Game/GameLogic/Player.h +++ b/Code/Game/GameLogic/Player.h @@ -48,7 +48,7 @@ namespace GameLogic void Respawn(Oyster::Math::Float3 spawnPoint); - void Rotate(const Oyster::Math3D::Float3 lookDir); + void Rotate(const Oyster::Math3D::Float4 lookDir); /******************************************************** * Collision function for player, this is to be sent to physics through the subscribe function with the rigidbody diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index 2413aa92..216dec73 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -69,6 +69,7 @@ namespace GameLogic float lookDirX; float lookDirY; float lookDirZ; + float deltaX; Protocol_PlayerLook() { @@ -78,6 +79,7 @@ namespace GameLogic this->protocol[1].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float; + this->protocol[4].type = Oyster::Network::NetAttributeType_Float; } Protocol_PlayerLook(Oyster::Network::CustomNetProtocol& p) @@ -85,12 +87,14 @@ namespace GameLogic lookDirX = p[1].value.netFloat; lookDirY = p[2].value.netFloat; lookDirZ = p[3].value.netFloat; + deltaX = p[4].value.netFloat; } const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val) { lookDirX = val[1].value.netFloat; lookDirY = val[2].value.netFloat; lookDirZ = val[3].value.netFloat; + deltaX = val[4].value.netFloat; return *this; } @@ -99,6 +103,8 @@ namespace GameLogic this->protocol[1].value = lookDirX; this->protocol[2].value = lookDirY; this->protocol[3].value = lookDirZ; + this->protocol[4].value = deltaX; + return &protocol; } diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index 8235c903..826cfd7c 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -172,10 +172,11 @@ namespace DanBias } void GameSession::Gameplay_PlayerLookDir ( Protocol_PlayerLook& p, DanBias::GameClient* c ) { - Oyster::Math3D::Float3 lookDir; + Oyster::Math3D::Float4 lookDir; lookDir.x = p.lookDirX; lookDir.y = p.lookDirY; lookDir.z = p.lookDirZ; + lookDir.w = p.deltaX; c->GetPlayer()->Rotate(lookDir); } void GameSession::Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c )