Az előadás letöltése folymat van. Kérjük, várjon

Az előadás letöltése folymat van. Kérjük, várjon

Motor IIII. Vezérlés Szécsi László. Letöltés diák: www.iit.bme.hu/~szecsi/GraphGame //l09-engine4.ppt.

Hasonló előadás


Az előadások a következő témára: "Motor IIII. Vezérlés Szécsi László. Letöltés diák: www.iit.bme.hu/~szecsi/GraphGame //l09-engine4.ppt."— Előadás másolata:

1 Motor IIII. Vezérlés Szécsi László

2 Letöltés diák: //l09-engine4.ppt

3 Új mechanizmus: vezérlés XML fileban definálható vezérlő RigidBodyControl osztály EngineCore-ba directory RigidBody-ba hivatkozás egy vezérlőre RigidBodyControl-ból származtatás –MotorControl –TargetControl betöltés

4 Új inline class: RigidBodyControl #pragma once class RigidBody; #include "ControlContext.h" /// Base class for rigid body control mechanisms. class RigidBodyControl { public: /// Applies control to the rigid body. Invoked from RigidBody::control, directly manipulates kinematic status. virtual void apply(RigidBody* controlledBody, const ControlContext& context)=0; };

5 RigidBody-ba class RigidBodyControl; class RigidBody : public Entity { friend class MotorControl; //majd kell RigidBodyControl* controller; public: void setController(RigidBodyControl* controller);

6 RigidBody.cpp void RigidBody::setController( RigidBodyControl* controller) { this->controller = controller; } RigidBody::RigidBody(ShadedMesh* shadedMesh, RigidModel* rigidModel) :Entity(shadedMesh) { controller = NULL;

7 Alkalmazzuk #include "RigidBodyControl.h" void RigidBody::control(const ControlContext& context){ force = D3DXVECTOR3(0.0, 0.0, 0.0); torque = D3DXVECTOR3(0.0, 0.0, 0.0); if(controller) controller->apply(this, context); }

8 Directory.h class RigidBodyControl; typedef std::map RigidBodyControlDirectory; Mielőtt be tudnánk tölteni valamit kell egy konkrét vezérlő

9 XML: ezt szeretnénk betölteni

10 XML … …

11 Új class: MotorControl base: RigidBodyControl #include "RigidBodyControl.h" class Motor; class MotorControl : public RigidBodyControl { /// Container of Motor instances. std::vector motors; public: /// Constructor. MotorControl(void); /// Destructor. Releases Motor instances. ~MotorControl(void); /// Inserts a motor. void addMotor(Motor* motor); void apply(RigidBody* controlledBody, const ControlContext& context); };

12 Új class: Motor class Motor { friend class MotorControl; friend class WheelControl; /// Activating key. unsigned int key; /// Force applied by motor. D3DXVECTOR3 force; /// Torque applied by motor. D3DXVECTOR3 torque; public: /// Constructor. Motor(unsigned int key, const D3DXVECTOR3& force, const D3DXVECTOR3& torque); };

13 Motor.cpp Motor::Motor(unsigned int key, const D3DXVECTOR3& force, const D3DXVECTOR3& torque) { this->key = key; this->force = force; this->torque = torque; }

14 MotorControl.cpp #include "MotorControl.h" #include "RigidBody.h" #include "RigidModel.h" #include "Motor.h" #include "ControlStatus.h" MotorControl::MotorControl(void){} MotorControl::~MotorControl(void) { std::vector ::iterator i = motors.begin(); while(i != motors.end()) { delete *i; i++; } void MotorControl::addMotor(Motor* motor) {motors.push_back(motor); }

15 MotorControl.cpp void MotorControl::apply(RigidBody* controlledBody, const ControlContext& context) { std::vector ::iterator i = motors.begin(); while(i != motors.end()) { Motor* motor = *i; if(context.controlStatus.keyPressed[motor->key]) { D3DXVECTOR3 worldForce, worldTorque; D3DXVec3TransformNormal(&worldForce, &motor->force, &controlledBody->rotationMatrix); controlledBody->force += worldForce; D3DXVec3TransformNormal(&worldTorque, &motor->torque, &controlledBody->rotationMatrix); controlledBody->torque += worldTorque; } i++;}

16 EngineCore-ba class MotorControl; //class előtt RigidBodyControlDirectory rigidBodyControlDirectory; void loadMotorControls( XMLNode& xMainNode); void loadMotors( XMLNode& motorControlNode, MotorControl* motorControl);

17 EngineCore.cpp #include "MotorControl.h" #include "Motor.h"

18 EngineCore.cpp void EngineCore::loadMotorControls(XMLNode& xMainNode) { int iMotorControl = 0; XMLNode motorControlNode; while( !(motorControlNode = xMainNode.getChildNode(L"MotorControl", iMotorControl)).isEmpty() ) { const wchar_t* motorControlName = motorControlNode|L"name"; MotorControl* motorControl = new MotorControl(); loadMotors(motorControlNode, motorControl); rigidBodyControlDirectory[motorControlName] = motorControl; iMotorControl++; }

19 EngineCore.cpp void EngineCore::loadMotors(XMLNode& motorControlNode, MotorControl* motorControl) { int iMotor = 0; XMLNode motorNode; while( !(motorNode = motorControlNode.getChildNode(L"Motor", iMotor)).isEmpty() ) { D3DXVECTOR3 force = motorNode.readVector(L"force"); D3DXVECTOR3 torque = motorNode.readVector(L"torque"); unsigned int keyCode = motorNode.readLong(L"key"); const wchar_t* codeTypeString = motorNode|L"codeType"; if(codeTypeString && wcscmp(codeTypeString, L"numpad") == 0) keyCode += VK_NUMPAD0; else if(codeTypeString && wcscmp(codeTypeString, L"function") == 0) keyCode += VK_F1; motorControl->addMotor( new Motor(keyCode, force, torque)); iMotor++; }

20 EngineCore::loadLevel loadMotorControls(xMainNode); //loadGroup elé

21 EngineCore::loadRigidBodies rigidBody->setPosition( rigidBodyNode.readVector(L"pos")); const wchar_t* controlName = rigidBodyNode|L"control"; if(controlName){ RigidBodyControlDirectory::iterator iControl = rigidBodyControlDirectory.find(controlName); if(iControl != rigidBodyControlDirectory.end()) rigidBody->setController(iControl->second); }

22 Mehet! Nehéz dolog az űrhajó-irányítás…

23 Legyen "légellenállás" Sebességfüggő csillapítás

24 RigidModel-be /// Coefficients of velocity attenuation. D3DXVECTOR3 drag; /// Coefficients of angular velocity attenuation. D3DXVECTOR3 angularDrag; public: void setDrag(const D3DXVECTOR3& drag); void setAngularDrag(const D3DXVECTOR3& angularDrag);

25 RigidModel.cpp void RigidModel::setDrag(const D3DXVECTOR3& drag) { this->drag = drag; } void RigidModel::setAngularDrag(const D3DXVECTOR3& angularDrag) { this->angularDrag = angularDrag; }

26 RigidModel::RigidModel drag = D3DXVECTOR3(0, 0, 0); angularDrag = D3DXVECTOR3(0, 0, 0);

27 EngineCore::loadRigidModels RigidModel* rigidModel = new RigidModel(invMass, invAngularMassX, invAngularMassY, invAngularMassZ); rigidModel->setDrag( rigidModelNode.readVector(L"drag")); rigidModel->setAngularDrag( rigidModelNode. readVector(L"angularDrag"));

28 RigidBody::control if(rigidModel->getInvMass() > 0.0) { D3DXMATRIX transposedRotationMatrix; D3DXMatrixTranspose(&transposedRotationMatrix, &rotationMatrix); D3DXVECTOR3 modelMomentum; D3DXVec3TransformNormal(&modelMomentum, &momentum, &transposedRotationMatrix); D3DXVECTOR3 velocity = modelMomentum * rigidModel->getInvMass(); D3DXVECTOR3 modelDragForce = D3DXVECTOR3( velocity.x * rigidModel->drag.x, velocity.y * rigidModel->drag.y, velocity.z * rigidModel->drag.z); modelDragForce *= D3DXVec3Length(&modelDragForce); D3DXVECTOR3 worldDragForce; D3DXVec3TransformNormal(&worldDragForce, &modelDragForce, &rotationMatrix); // folyt.

29 Visszacsapás megakadályozása double expectedMomentumChange = D3DXVec3Length(&(worldDragForce * context.dt)); double allowedMomentumChange = D3DXVec3Length(&momentum) * 0.5; if( expectedMomentumChange > allowedMomentumChange) worldDragForce *= allowedMomentumChange / expectedMomentumChange; force -= worldDragForce; // folyt.

30 Ugyanez forgásra D3DXVECTOR3 modelAngularMomentum; D3DXVec3TransformNormal(&modelAngularMomentum, &angularMomentum, &transposedRotationMatrix); D3DXVECTOR3 angularVelocity; D3DXVec3TransformNormal(&angularVelocity, &modelAngularMomentum, &rigidModel->getInvAngularMass()); D3DXVECTOR3 modelDragTorque = D3DXVECTOR3( angularVelocity.x * rigidModel->angularDrag.x, angularVelocity.y * rigidModel->angularDrag.y, angularVelocity.z * rigidModel->angularDrag.z); modelDragTorque *= D3DXVec3Length(&modelDragTorque); D3DXVECTOR3 worldDragTorque; D3DXVec3TransformNormal(&worldDragTorque, &modelDragTorque, &rotationMatrix); double expectedAngularMomentumChange = D3DXVec3Length(&(worldDragTorque * context.dt)); double allowedAngularMomentumChange = D3DXVec3Length(&angularMomentum) * 0.5; if( expectedAngularMomentumChange > allowedAngularMomentumChange ) worldDragTorque *= allowedAngularMomentumChange / expectedAngularMomentumChange; torque -= worldDragTorque; } // endif(rigidModel->getInvMass() > 0.0)

31 Mehet! Így már könnyebb.


Letölteni ppt "Motor IIII. Vezérlés Szécsi László. Letöltés diák: www.iit.bme.hu/~szecsi/GraphGame //l09-engine4.ppt."

Hasonló előadás


Google Hirdetések