btConvexcastBatch (bool unused, btScalar ray_length, btScalar min_z, btScalar max_z, btScalar min_y , btScalar max_y ) : boxShape(btVector3(0.0, 0.0, 0.0)) { boxShapeHalfExtents = btVector3(1.0, 1.0, 1.0); boxShape = btBoxShape(boxShapeHalfExtents); frame_counter = 0; ms = 0; max_ms = 0; min_ms = 9999.0; sum_ms_samples = 0; sum_ms = 0; dx = 10.0; min_x = -40; max_x = 20; this->min_y = min_y; this->max_y = max_y; sign = 1.0; btScalar dalpha = 2*SIMD_2_PI/NUMRAYS_IN_BAR; for (int i = 0; i < NUMRAYS_IN_BAR; i++) { btScalar z = (max_z-min_z)/NUMRAYS_IN_BAR * i + min_z; source[i] = btVector3(min_x, max_y, z); dest[i] = btVector3(min_x + ray_length, min_y, z); normal[i] = btVector3(1.0, 0.0, 0.0); } }
btConvexcastBatch (btScalar ray_length, btScalar z, btScalar min_y = -1000, btScalar max_y = 10) : boxShape(btVector3(0.0, 0.0, 0.0)) { boxShapeHalfExtents = btVector3(1.0, 1.0, 1.0); boxShape = btBoxShape(boxShapeHalfExtents); frame_counter = 0; ms = 0; max_ms = 0; min_ms = 9999.0; sum_ms_samples = 0; sum_ms = 0; dx = 10.0; min_x = -40; max_x = 20; this->min_y = min_y; this->max_y = max_y; sign = 1.0; btScalar dalpha = 2*SIMD_2_PI/NUMRAYS_IN_BAR; for (int i = 0; i < NUMRAYS_IN_BAR; i++) { btScalar alpha = dalpha * i; // rotate around by alpha degrees y btQuaternion q(btVector3(0.0, 1.0, 0.0), alpha); direction[i] = btVector3(1.0, 0.0, 0.0); direction[i] = q * direction[i]; direction[i] = direction[i]; source[i] = btVector3(min_x, max_y, z); dest[i] = source[i] + direction[i] * ray_length; dest[i][1] = min_y; normal[i] = btVector3(1.0, 0.0, 0.0); } }
u32 wsScene::addModel(wsModel* myModel) { u32 modelHash = wsHash(myModel->getName()); if (models->insert(modelHash, myModel) == WS_SUCCESS) { #if WS_PHYSICS_BACKEND == WS_BACKEND_BULLET const vec4 dimensions = myModel->getBounds(); btCollisionShape* boundsShape; if (myModel->getCollisionShape() == WS_NULL) { boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(dimensions.x, dimensions.y, dimensions.z))); } else { wsCollisionShape* shape = myModel->getCollisionShape(); switch (shape->getType()) { default: boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(dimensions.x, dimensions.y, dimensions.z))); break; case WS_SHAPE_CAPSULE: boundsShape = wsNew(btCapsuleShape, btCapsuleShape(shape->getDim0(), shape->getDim1())); break; case WS_SHAPE_CUBE: boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(shape->getDim0(), shape->getDim1(), shape->getDim2()))); break; case WS_SHAPE_CYLINDER: boundsShape = wsNew(btCylinderShape, btCylinderShape(btVector3(shape->getDim0(), shape->getDim1()/2.0f, shape->getDim0()))); break; case WS_SHAPE_SPHERE: boundsShape = wsNew(btSphereShape, btSphereShape(shape->getDim0())); break; } } f32 mass = myModel->getMass(); btVector3 myInertia(0.0f, 0.0f, 0.0f); boundsShape->calculateLocalInertia(mass, myInertia); btRigidBody::btRigidBodyConstructionInfo modelRigidBodyCI(mass, myModel->getTransformp(), boundsShape, myInertia); btRigidBody* modelRigidBody = wsNew(btRigidBody, btRigidBody(modelRigidBodyCI)); if (myModel->getProperties() & WS_MODEL_LOCK_HORIZ_ROTATIONS) { modelRigidBody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f)); } modelRigidBody->setActivationState(DISABLE_DEACTIVATION); rigidBodies->insert(modelHash, modelRigidBody); physicsWorld->addRigidBody(modelRigidBody, myModel->getCollisionClass(), collisionClasses[(u32)wsLog2(myModel->getCollisionClass())]); #endif return modelHash; } return WS_NULL; }// End addModel(wsModel*) method
void wsScene::addAnimation(wsAnimation* myAnim, const char* modelName) { wsModel* myModel = models->retrieve(wsHash(modelName)); myModel->addAnimation(myAnim); #if WS_PHYSICS_BACKEND == WS_BACKEND_BULLET char animName[512]; strcpy(animName, modelName); strcat(animName, myAnim->getName()); u32 boundsHash = wsHash(animName); const vec4 dimensions = myAnim->getBounds(); btCollisionShape* boundsShape; if (myModel->getCollisionShape() == WS_NULL) { boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(dimensions.x, dimensions.y, dimensions.z))); } else { wsCollisionShape* shape = myModel->getCollisionShape(); switch (shape->getType()) { default: boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(dimensions.x, dimensions.y, dimensions.z))); break; case WS_SHAPE_CAPSULE: boundsShape = wsNew(btCapsuleShape, btCapsuleShape(shape->getDim0(), shape->getDim1())); break; case WS_SHAPE_CUBE: boundsShape = wsNew(btBoxShape, btBoxShape(btVector3(shape->getDim0(), shape->getDim1(), shape->getDim2()))); break; case WS_SHAPE_CYLINDER: boundsShape = wsNew(btCylinderShape, btCylinderShape(btVector3(shape->getDim0(), shape->getDim1()/2.0f, shape->getDim0()))); break; case WS_SHAPE_SPHERE: boundsShape = wsNew(btSphereShape, btSphereShape(shape->getDim0())); break; } } f32 mass = myModel->getMass(); btVector3 myInertia(0.0f, 0.0f, 0.0f); boundsShape->calculateLocalInertia(mass, myInertia); btRigidBody::btRigidBodyConstructionInfo animRigidBodyCI(mass, myModel->getTransformp(), boundsShape, myInertia); btRigidBody* animRigidBody = wsNew(btRigidBody, btRigidBody(animRigidBodyCI)); if (myModel->getProperties() & WS_MODEL_LOCK_HORIZ_ROTATIONS) { animRigidBody->setAngularFactor(btVector3(0.0f, 0.0f, 0.0f)); } rigidBodies->insert(boundsHash, animRigidBody); #endif }// End addAnimation method
bool QCharacterController::inAir() { Ogre::Quaternion quat = Ogre::Root::getSingletonPtr()->getSceneManager("mSceneManager")->getCamera("mMainCam")->getDerivedOrientation(); btQuaternion qu; qu.setX(quat.x); qu.setY(quat.y); qu.setZ(quat.z); qu.setW(quat.w); mInAirLast2 = false; OverlapResultCallback2 overlapCall = OverlapResultCallback2(); overlapCall.m_collisionFilterGroup = QE::COL_STATICS; overlapCall.m_collisionFilterMask = QE::COL_STATICS|QE::COL_DYNAMICS; // just a guess... but half extents of the PhysX one...? btBoxShape sphsh = btBoxShape(btVector3(0.25f,(mController->cctp[0]->radius*0.6525f),0.25f)); btCollisionObject* bcobj = new btCollisionObject(); btVector3 boxPosition = mController->cctp[0]->getPosition()-0.5f*mController->cctp[0]->radius*mController->upVector; bcobj->setWorldTransform(btTransform(qu,boxPosition)); bcobj->setCollisionShape(&sphsh); this->mPhysicsManager->getScene()->contactTest(bcobj,overlapCall); delete bcobj; //unsigned int a = 5;//mController->Scene->overlapOBBShapes(NxBox(mController->cctp[0]->getPosition()-0.5f*mController->cctp[0]->radius*mController->upVector,NxVec3(0.25f,mController->cctp[0]->radius*0.6525f,0.25f),NxMat33(qu)),NX_ALL_SHAPES,0,NULL,NULL,0|1|2|5,0,true); bool tInAir = true; if(overlapCall.hits>0) { tInAir = false; mInAirLast2 = false; } if(!tInAir&&mInAirLast) { mMidairTimeBase = clock()/1000.0f; } else if(!tInAir) { mMidairTime = clock()/1000.0f; if(mMidairTime-mMidairTimeBase>0.05f&&mInMidair) { mInMidair = false; mInAirLast2 = true; } } else { mInMidair = true; mMidairTimeBase = 0; mMidairTime = 0; } mInAirLast = tInAir; return tInAir; }
EventBox::EventBox(vec3f size, CameraLogic* logic, PlayerLogic* playerLogic) :m_Event((std::string("Event Box") + std::to_string(s_Count)).c_str()), m_Handler(logic, playerLogic), m_Box(0, New btDefaultMotionState(), New btBoxShape(btVector3(size.x, size.y, size.z) * 0.5f / Config::s_PPM)), m_Model("") { s_Count++; m_Event.AddComponent(&m_Handler); m_Event.AddComponent(&m_Box); m_Event.AddComponent(&m_Model); m_Node = New WE::Node(&m_Event); m_Event.AddComponent(m_Node); m_Box.GetBody()->setCollisionFlags(m_Box.GetBody()->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE); }
Enemy::Enemy(std::string path, vec3f size,vec3f modelSize, vec3f tunnelSize, unsigned int count, unsigned int current, Player* player, EnemyPool* enemies, CameraLogic* camera) : m_Enemy("Enemy"), m_Model(path.c_str(),modelSize), m_Body(1, New btDefaultMotionState(), New btBoxShape(btVector3(size.x, size.y, size.z) / 2.0f)), m_Node(nullptr), m_Logic(&m_Body), m_Handler(player, enemies,camera) { m_Enemy.AddComponent(&m_Model); m_Enemy.AddComponent(&m_Body); m_Node = New Node(&m_Enemy); m_Enemy.AddComponent(m_Node); InitialisePosition(tunnelSize,size,count, current); m_Body.GetRigidBody().setLinearFactor(btVector3(1.0f, 0.0f, 1.0f)); m_Body.GetRigidBody().setCollisionFlags(m_Body.GetRigidBody().getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE); m_Enemy.AddComponent(&m_Logic); m_Enemy.AddComponent(&m_Handler); }
u32 wsScene::addPrimitive(wsPrimitive* myPrimitive) { wsAssert(numPrimitives < WS_MAX_PRIMITIVES, "Cannot add another primitive to the scene. Maximum already reached."); primitives[numPrimitives] = myPrimitive; #if WS_PHYSICS_BACKEND == WS_BACKEND_BULLET switch (myPrimitive->getType()) { case WS_PRIM_TYPE_PLANE: { wsPlane* plane = (wsPlane*)myPrimitive; vec4 data = plane->getPosData(); btCollisionShape* planeShape = wsNew(btStaticPlaneShape, btStaticPlaneShape(btVector3(data.x, data.y, data.z), data.w)); btDefaultMotionState* planeState = wsNew(btDefaultMotionState, btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f), btVector3(0.0f, 0.0f, 0.0f)))); btRigidBody::btRigidBodyConstructionInfo planeRigidBodyCI(0, planeState, planeShape, btVector3(0,0,0)); btRigidBody* groundRigidBody = wsNew(btRigidBody, btRigidBody(planeRigidBodyCI)); physicsWorld->addRigidBody(groundRigidBody, myPrimitive->getCollisionClass(), collisionClasses[(u32)wsLog2(myPrimitive->getCollisionClass())]); } break; case WS_PRIM_TYPE_CUBE: { wsCube* cube = (wsCube*)myPrimitive; vec4 dimensions = cube->getDimensions(); vec4 pos = cube->getPos(); quat rot = cube->getRot(); btCollisionShape* cubeShape = wsNew(btBoxShape, btBoxShape(btVector3(dimensions.x/2.0f, dimensions.y/2.0f, dimensions.z/2.0f))); btDefaultMotionState* cubeState = wsNew(btDefaultMotionState, btDefaultMotionState(btTransform(btQuaternion(rot.x, rot.y, rot.z, rot.w), btVector3(pos.x, pos.y, pos.z)))); btRigidBody::btRigidBodyConstructionInfo cubeRigidBodyCI(0, cubeState, cubeShape, btVector3(0, 0, 0)); btRigidBody* cubeRigidBody = wsNew(btRigidBody, btRigidBody(cubeRigidBodyCI)); physicsWorld->addRigidBody(cubeRigidBody, myPrimitive->getCollisionClass(), collisionClasses[(u32)wsLog2(myPrimitive->getCollisionClass())]); } break; default: break; } #endif return numPrimitives++; }