Előadást letölteni
Az előadás letöltése folymat van. Kérjük, várjon
KiadtaEde Molnár Megváltozta több, mint 10 éve
1
Motor IIII. Vezérlés Szécsi László
2
Letöltés diák: www.iit.bme.hu/~szecsi/GraphGame //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 <Motor key="5" codeType="numpad" force.x="35" force.y="0" force.z="0" /> <Motor key="0" codeType="numpad" force.x="-35" force.y="0" force.z="0" /> <Motor key="7" codeType="numpad" torque.x="1" torque.y="0" torque.z="0" /> <Motor key="9" codeType="numpad" torque.x="-1" torque.y="0" torque.z="0" /> <Motor key="6" codeType="numpad" torque.x="0" torque.y="1" torque.z="0" /> <Motor key="4" codeType="numpad" torque.x="0" torque.y="-1" torque.z="0" /> <Motor key="8" codeType="numpad" torque.x="0" torque.y="0" torque.z="1" /> <Motor key="2" codeType="numpad" torque.x="0" torque.y="0" torque.z="-1" />
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 <RigidModel name="ship" invMass="1" invAngularMassX="1" invAngularMassY="1" invAngularMassZ="1" drag.x="0.2" drag.y="1" drag.z="1" angularDrag.x="5" angularDrag.y="5" angularDrag.z="5" />
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.
Hasonló előadás
© 2024 SlidePlayer.hu Inc.
All rights reserved.