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. PhysX utáni rendberakás Vezérlés Szécsi László.

Hasonló előadás


Az előadások a következő témára: "Motor IIII. PhysX utáni rendberakás Vezérlés Szécsi László."— Előadás másolata:

1 Motor IIII. PhysX utáni rendberakás Vezérlés Szécsi László

2 Űrhajók physX-xel írjuk át a korábbi xml-t PhysicsEntity-kre hasaljunk be valami közelítő geometriát (pl. NxCapsuleShape)

3 PhysX remote debugger nxPhysicsSDK = NxCreatePhysicsSDK( NX_PHYSICS_SDK_VERSION); // Hook the Application with the Visual Debugger nxPhysicsSDK-> getFoundationSDK(). getRemoteDebugger()-> connect ("localhost", 5425);

4 Entity metódusai mindet valósítsuk meg a PhysicsEntity-ben trafo lekérdezése az Actortól (lásd render metódus) ebből a kért trafó számítása kb. ugyanúgy mint az Entityben

5 idáig önálló munka a vezérléssel folytatjuk

6 Hajó közelítő geometriája

7 Új mechanizmus: vezérlés XML fileban definálható vezérlő Controller osztály EngineCore-ba directory PhysicsEntity-be hivatkozás egy vezérlőre Controller-ből származtatás –MotorController –TargetController betöltés

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

9 PhysicsEntity-be class Controller; class PhysicsEntity : public Entity { Controller* controller; public: void setController(Controller* controller);

10 PhysicsEntity.cpp void PhysicsEntity::setController( Controller* controller) { this->controller = controller; } PhysicsEntity::PhysicsEntity(…) :Entity(shadedMesh) { controller = NULL;

11 Alkalmazzuk #include "Controller.h" void PhysicsEntity::control(const ControlContext& context){ if(controller) controller->apply(this, context); }

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

13 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" />

14 XML … …

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

16 Új class: Motor class Motor { friend class MotorController; friend class WheelController; /// Activating key. unsigned int key; /// Force applied by motor. NxVec3 force; /// Torque applied by motor. NxVec3 torque; public: /// Constructor. Motor(unsigned int key, const NxVec3& force, const NxVec3& torque); };

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

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

19 MotorController.cpp void MotorController::apply(PhysicsEntity* controlledEntity, const ControlContext& context) { std::vector ::iterator i = motors.begin(); while(i != motors.end()) { Motor* motor = *i; if(context.controlStatus.keyPressed[motor->key]) { NxActor* actor = controlledEntity->getActor(); actor->addLocalForce(motor->force); actor->addLocalTorque(motor->torque); } i++;}

20 EngineCore-ba class MotorController; //class előtt ControllerDirectory controllerDirectory; void loadMotorControllers( XMLNode& xMainNode); void loadMotors( XMLNode& motorControllerNode, MotorController* motorController);

21 EngineCore.cpp #include "MotorController.h" #include "Motor.h"

22 EngineCore.cpp void EngineCore::loadMotorControllers(XMLNode& xMainNode) { int iMotorController = 0; XMLNode motorControllerNode; while( !(motorControllerNode = xMainNode.getChildNode(L"MotorController", iMotorController)).isEmpty() ) { const wchar_t* motorControllerName = motorControllerNode|L"name"; MotorController* motorController = new MotorController(); loadMotors(motorControllerNode, motorControl); controllerDirectory[motorControllerName] = motorController; iMotorController++; }

23 EngineCore.cpp void EngineCore::loadMotors(XMLNode& motorControllerNode, MotorControl* motorControl) { int iMotor = 0; XMLNode motorNode; while( !(motorNode = motorControllerNode.getChildNode(L"Motor", iMotor)).isEmpty() ) { NxVec3 force = motorNode.readNxVec3(L"force"); NxVec3 torque = motorNode.readNxVec3(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; motorController->addMotor( new Motor(keyCode, force, torque)); iMotor++; }

24 EngineCore::loadLevel loadMotorControllers(xMainNode); //loadGroup elé

25 EngineCore::loadPhysicsEntities PhysicsEntity* physicsEntity = new PhysicsEntity(iShadedMesh->second, iPhysicsModel->second, nxScene, position); const wchar_t* controllerName = physicsEntityNode|L"controller"; if(controllerName){ ControllerDirectory::iterator iController = controllerDirectory.find(controllerName); if(iControler != controllerDirectory.end()) physicsEntity-> setController(iControl->second); }

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

27 Legyen "légellenállás" Sebességfüggő csillapítás <PhysicsModel name="ship" drag="1" angularDrag="5" />

28 PhysicsModel-be public: void setDrag(doubledrag); void setAngularDrag( double angularDrag);

29 PhysicsModel.cpp void PhysicsModel::setDrag(double drag) { nxActorDesc.body->linearDamping = drag; } void PhysicsModel::setAngularDrag( double angularDrag) { nxActorDesc.body->angularDamping = angularDrag; }

30 EngineCore::loadPhysicsModels physicsModel->setDrag( physicsModelNode.readDouble(L"drag") ); physicsModel->setAngularDrag( physicsModelNode. readDouble(L"angularDrag"));

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

32 XML

33 Új class: Target class Entity; class Target { friend class TargetController; D3DXVECTOR3 position; double radius; Entity* mark; public: Target(const D3DXVECTOR3& position, double radius); void setMark(Entity* mark); };

34 Target.cpp Target::Target(const D3DXVECTOR3& position, double radius) { mark = NULL; this->radius = radius; this->position = position; } void Target::setMark(Entity* mark) { this->mark = mark; }

35 Új class: TargetController #include class Target; class TargetController : public Controller { std::vector targets; std::vector ::iterator currentTarget; double maxForce; double maxTorque; public: TargetControl(double maxForce, double maxTorque); ~TargetControl(void); void addTarget(Target* target); void apply(PhysicsEntity* controlledEntity, const ControlContext& context); };

36 TargetController.cpp TargetController::TargetController(do uble maxForce, double maxTorque) { currentTarget = targets.end(); this->maxForce = maxForce; this->maxTorque = maxTorque; }

37 TargetController.cpp void TargetController::addTarget(Target* target) { targets.push_back(target); currentTarget = targets.begin(); } TargetController::~TargetController(void) { std::vector ::iterator i = targets.begin(); while(i != targets.end()) { delete *i; i++; }

38 TargetController::apply void TargetController::apply(PhysicsEntity* controlledEntity, const ControlContext& context) { if(!targets.empty()) { Target* target = *currentTarget; D3DXVECTOR3 markDifference = target->position - controlledBody->position; if(target->mark) markDifference += target->mark->getPosition(); if(D3DXVec3Length(&markDifference) radius) { currentTarget++; if(currentTarget == targets.end()) currentTarget = targets.begin(); }

39 TargetController::apply D3DXVECTOR3 markDirection; D3DXVec3Normalize(&markDirection, &markDifference); D3DXMATRIX modelMatrix; controlledBody->getModelMatrix(modelMatrix); D3DXVECTOR3 ahead; D3DXVec3TransformNormal(&ahead, &D3DXVECTOR3(1, 0, 0), &modelMatrix); D3DXVECTOR3 turnAxis; D3DXVec3Cross(&turnAxis, &ahead, &markDirection); controlledEntity->getActor()->addTorque(*(NxVec3*)&( turnAxis * maxTorque); controlledEntity->getActor()- >addForce(*(NxVec3*)&(ahead * D3DXVec3Dot(&ahead, &markDirection) * maxForce); }

40 EngineCore // vezérlők legyártása entitások betöltése előtt void loadTargetControllers(XMLNode& xMainNode); // célpontok bekötése entitások betöltése után void finishTargetControllers(XMLNode& xMainNode); void loadTargets(XMLNode& targetControllerNode, TargetController* targetController);

41 EngineCore::loadTargetControllers void EngineCore::loadTargetControllers(XMLNode& xMainNode) { int iTargetController = 0; XMLNode targetControllerNode; while( !(targetControllerNode = xMainNode.getChildNode(L"TargetController", iTargetController)).isEmpty() ) { const wchar_t* targetControllerName = targetControllerNode|L"name"; TargetController* targetController = new TargetController( targetControllerNode.readDouble(L"maxForce"), targetControllerNode.readDouble(L"maxTorque")); controllerDirectory[targetControllerName] = targetController; iTargetController++; }

42 EngineCore:: finishTargetControllers void EngineCore::finishTargetControllers(XMLNode& xMainNode) { int iTargetController = 0; XMLNode targetControllerNode; while( !(targetControllerNode = xMainNode.getChildNode(L"targetController", iTargetController)).isEmpty() ) { const wchar_t* targetControllerName = targetControllerNode|L"name"; TargetController* targetController = (TargetController*)controlDirectory [targetControllerName]; loadTargets(targetControllerNode, targetController); iTargetControl++; }

43 EngineCore::loadTargets void EngineCore::loadTargets(XMLNode& targetControlNode, TargetController* targetController) { int iTarget = 0; XMLNode targetNode; while( !(targetNode = targetControllerNode.getChildNode(L"Target", iTarget)).isEmpty() ) { D3DXVECTOR3 pos = targetNode.readVector(L"position"); double radius = targetNode.readDouble(L"radius", 10); Target* target = new Target(pos, radius); const wchar_t* markEntityName = targetNode|L"mark"; if(markEntityName != NULL) { EntityDirectory::iterator iEntity = entityDirectory.find(markEntityName); if(iEntity != entityDirectory.end()) target->setMark(iEntity->second); } targetController->addTarget(target); iTarget++; }

44 EngineCore::loadLevel loadTargetControllers(xMainNode); XMLNode groupNode = xMainNode.getChildNode(L"Group"); sceneRoot = NULL; loadGroup(groupNode, sceneRoot); finishTargetControllers(xMainNode);

45 XML


Letölteni ppt "Motor IIII. PhysX utáni rendberakás Vezérlés Szécsi László."

Hasonló előadás


Google Hirdetések