PolyPhysicsSceneEntity.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * PolyPhysicsSceneEntity.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 6/17/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyPhysicsSceneEntity.h"
  10. using namespace Polycode;
  11. PhysicsCharacter::PhysicsCharacter(SceneEntity *entity, float mass, float friction, float stepSize) : PhysicsSceneEntity(entity, PhysicsSceneEntity::CHARACTER_CONTROLLER, mass, friction, 1) {
  12. ghostObject = new btPairCachingGhostObject();
  13. Vector3 pos = entity->getPosition();
  14. btTransform transform;
  15. transform.setIdentity();
  16. transform.setOrigin(btVector3(pos.x,pos.y,pos.z));
  17. ghostObject->setWorldTransform(transform);
  18. ghostObject->setCollisionShape (shape);
  19. ghostObject->setFriction(friction);
  20. ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);
  21. character = new btKinematicCharacterController (ghostObject,convexShape,btScalar(stepSize));
  22. }
  23. void PhysicsCharacter::setWalkDirection(Vector3 direction) {
  24. character->setWalkDirection(btVector3(direction.x, direction.y, direction.z));
  25. }
  26. void PhysicsCharacter::jump() {
  27. character->jump();
  28. }
  29. void PhysicsCharacter::Update() {
  30. btVector3 pos = ghostObject->getWorldTransform().getOrigin();
  31. sceneEntity->setPosition(pos.x(), pos.y(), pos.z());
  32. // sceneEntity->rebuildTransformMatrix();
  33. sceneEntity->dirtyMatrix(true);
  34. }
  35. PhysicsCharacter::~PhysicsCharacter() {
  36. }
  37. PhysicsSceneEntity::PhysicsSceneEntity(SceneEntity *entity, int type, float mass, float friction, float restitution) : CollisionSceneEntity(entity, false, type) {
  38. this->mass = mass;
  39. btVector3 localInertia(0,0,0);
  40. Vector3 pos = entity->getPosition();
  41. btTransform transform;
  42. transform.setIdentity();
  43. transform.setOrigin(btVector3(pos.x,pos.y,pos.z));
  44. if(mass != 0.0f) {
  45. shape->calculateLocalInertia(mass,localInertia);
  46. }
  47. if(type == CHARACTER_CONTROLLER) {
  48. } else {
  49. btDefaultMotionState* myMotionState = new btDefaultMotionState(transform);
  50. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia);
  51. rigidBody = new btRigidBody(rbInfo);
  52. // rigidBody->setActivationState(ISLAND_SLEEPING);
  53. rigidBody->setFriction(friction);
  54. rigidBody->setRestitution(restitution);
  55. }
  56. }
  57. void PhysicsSceneEntity::Update() {
  58. Matrix4 m;
  59. btScalar* mat = (btScalar*) malloc(sizeof(btScalar) * 16);
  60. rigidBody->getWorldTransform().getOpenGLMatrix(mat);
  61. for(int i=0; i < 16; i++) {
  62. m.ml[i] = mat[i];
  63. }
  64. free(mat);
  65. sceneEntity->setTransformByMatrixPure(m);
  66. }
  67. SceneEntity *PhysicsSceneEntity::getSceneEntity() {
  68. return sceneEntity;
  69. }
  70. PhysicsSceneEntity::~PhysicsSceneEntity() {
  71. }