Ejemplo n.º 1
0
void Character::checkCollision()
{
	isGrounded=false;
	for (Rect* g: *grounds)
	{
		if (g->getMidX() - draw_Sprite->getPositionX()<1500 && 
			g->getMidY() - draw_Sprite->getPositionY()<1500 )
		{

			checkGround(g);
			checkTop(g);
			checkSide(true,g);
			checkSide(false,g);
		}
	}

	if (collidingWith==nullptr){
		
	}
	else if (rCollider.intersectsRect(*collidingWith)){

	}
	else if (lCollider.intersectsRect(*collidingWith)){

	}
	else
	{
		collidingWith==nullptr;
	}

}
Ejemplo n.º 2
0
void object::applyGravity() {
    speed += acceleration + gravity;
    acceleration.clear();
    for( u8 i = 0; i < (1 << 3); i += 1)
    {
        checkGround();
        checkSides();
        position += (speed / 8);
    }
}
Ejemplo n.º 3
0
// Basically the update method for the Player class. 
void 
Player::Think(float time)
{

#pragma region Kinect

	//drawSkeleton();

	//drawHitBox("HitBox", mPlayerObject->fallRigidBody);

	//initHitBox = true;

#pragma endregion End of Kinect code/Not used right now   

#pragma region Controls 


	if (start) {

		

		if (canJump)
			playAnimation("idle1", time);

		if (mKinect->getEnableKinect())
		{
			// JUMP
			Ogre::Real jump = mKinect->detectJump();
			if (jump == 0 || jump == 1 || jump == 2)
			{
				playAnimation("fall_idle", time);

				btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
					mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

				if (canJump)
				{
					vector<Ogre::Vector3> skeletonNodes = mKinect->getSkeletonNodes();

					Ogre::Real jumpHeightPercentage = 1.0;

					// LEFT FOOT LIFT JUMP
					if (jump == 0)
					{
						jumpHeightPercentage = Ogre::Math::Abs(mKinect->initialPositions[NUI_SKELETON_POSITION_FOOT_LEFT].y - 
							skeletonNodes[NUI_SKELETON_POSITION_FOOT_LEFT].y) / mKinect->leftLegLiftMax;

					}
					// RIGHT FOOT LIFT JUMP
					else if (jump == 1)
					{
						jumpHeightPercentage = Ogre::Math::Abs(mKinect->initialPositions[NUI_SKELETON_POSITION_FOOT_RIGHT].y - 
							skeletonNodes[NUI_SKELETON_POSITION_FOOT_RIGHT].y) / mKinect->rightLegLiftMax;
					}
					// BOTH FEET OFF GROUND JUMP

					else if (jump == 2)
					{
						jumpHeightPercentage = ((Ogre::Math::Abs(mKinect->initialPositions[NUI_SKELETON_POSITION_FOOT_LEFT].y - 
							skeletonNodes[NUI_SKELETON_POSITION_FOOT_LEFT].y) + 
							Ogre::Math::Abs(mKinect->initialPositions[NUI_SKELETON_POSITION_FOOT_RIGHT].y - 
							skeletonNodes[NUI_SKELETON_POSITION_FOOT_RIGHT].y)) / 2) / 
							((mKinect->leftLegLiftMax + mKinect->rightLegLiftMax) / 2);
					}

					if (jumpHeightPercentage <= 0.0 || jumpHeightPercentage > 1.2 || jump == -1)
						jumpHeightPercentage = 1.0;

					Ogre::Real jumpHeight = 125 * jumpHeightPercentage;

					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * 40, jumpHeight, currCameraPos.getZ() * 40));			
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * 40, jumpHeight, currCameraPos.getZ() * 40));	
				}
			}

			else if (canJump)
			{
				if (mKinect->detectArm() == 0)
				{
					mWorld->display->displayHint(false);
					mWorld->display->displayEnding(false);
				}

				Ogre::Real sway = mKinect->detectSway();

				// RIGHT
				if (sway > 0.0)
				{
					playAnimation("strafeRight", time);

					Ogre::Real swayRightPercentage = sway / mKinect->swayRightMax;

					if (swayRightPercentage <= 0.0 || swayRightPercentage > 1.2)
						swayRightPercentage = 1.0;

					Ogre::Real strafeRightSpeed = 50 * swayRightPercentage; 

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealRight().x, 
						mCamera->mRenderCamera->getRealRight().y, mCamera->mRenderCamera->getRealRight().z); 

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,0,0));


					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * strafeRightSpeed, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * strafeRightSpeed));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * strafeRightSpeed,
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * strafeRightSpeed));
				}
				// LEFT
				else if (sway < 0.0)
				{
					playAnimation("strafeLeft", time);

					Ogre::Real swayLeftPercentage = sway / mKinect->swayLeftMax;

					if (swayLeftPercentage <= 0.0 || swayLeftPercentage > 1.2)
						swayLeftPercentage = 1.0;

					Ogre::Real strafeLeftSpeed = 50 * swayLeftPercentage; 

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealRight().x, 
						mCamera->mRenderCamera->getRealRight().y, mCamera->mRenderCamera->getRealRight().z); 

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,0,0));
					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * -strafeLeftSpeed, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -strafeLeftSpeed));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * -strafeLeftSpeed,
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -strafeLeftSpeed));

				}

				Ogre::Real turn = mKinect->detectTurn();

				if (turn < 0.0)
				{
					playAnimation("turnLeft", time);

					Ogre::Real turnLeftPercentage = turn / mKinect->leftRotationMax;

					if (turnLeftPercentage <= 0.0 || turnLeftPercentage > 1.2)
						turnLeftPercentage = 1.0;

					Ogre::Real turnLeftSpeed = turnLeftPercentage;

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,-turnLeftSpeed,0));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(0,
						mPhysManager->_world->getGravity().getY() + 70,0));
				}

				else if (turn > 0.0)
				{
					playAnimation("turnRight", time);

					Ogre::Real turnRightPercentage = turn / mKinect->rightRotationMax;

					if (turnRightPercentage <= 0.0 || turnRightPercentage > 1.2)
						turnRightPercentage = 1.0;

					Ogre::Real turnRightSpeed = turnRightPercentage;

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,turnRightSpeed,0));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(0,
						mPhysManager->_world->getGravity().getY() + 70,0));
				}

				Ogre::Real lean = mKinect->detectLean();

				if (lean < 0.0)
				{

					Ogre::Real leanForwardPercentage = Ogre::Math::Abs(lean / mKinect->leanForwardMax);

					if (leanForwardPercentage <= 0.0 || leanForwardPercentage > 1.2)
						leanForwardPercentage = 1.0;

					Ogre::Real forwardSpeed = leanForwardPercentage * 80;

					if (Ogre::Math::Abs(leanForwardPercentage) < 0.6)
					{
						playAnimation("walkForward", time);

						btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
							mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

						mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX(), 
							mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()));

						mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * forwardSpeed, 
							mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * forwardSpeed)); 
					}
					else if (Ogre::Math::Abs(leanForwardPercentage) > 0.6)
					{
						playAnimation("run", time);

						btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
							mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

						mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX(), 
							mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()));

						mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * forwardSpeed, 
							mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * forwardSpeed)); 
					}
				}

				else if (lean > 0.0)
				{
					playAnimation("walkBackward", time);

					Ogre::Real leanBackPercentage = lean / mKinect->leanBackMax;

					if (leanBackPercentage <= 0.0 || leanBackPercentage > 1.2)
						leanBackPercentage = 1.0;

					Ogre::Real backSpeed = leanBackPercentage * 50;

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
						mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * -1, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()) * -1);
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * -backSpeed, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -backSpeed)); 

				}
			}
		}

		// If the keyboard is enabled 
		if (mEnableKeyboard) 
		{	

			// turn off all overlays 
			if (mInputHandler->IsKeyDown(OIS::KC_RETURN))
			{
				mWorld->display->displayHint(false);
				mWorld->display->displayEnding(false);
			}

			// Jump
			if (mInputHandler->IsKeyDown(OIS::KC_SPACE))
			{
				playAnimation("fall_idle", time);

				btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
					mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

				if (canJump)
				{
					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * 90, 125, currCameraPos.getZ() * 90));			
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * 90, 125, currCameraPos.getZ() * 90));			
				}
			}

			if (canJump) 
			{
				// Left 
				if (mInputHandler->IsKeyDown(OIS::KC_M))
				{
					playAnimation("strafeRight", time);

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealRight().x, 
						mCamera->mRenderCamera->getRealRight().y, mCamera->mRenderCamera->getRealRight().z); 

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,0,0));


					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * 40, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * 40));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * 40,
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * 40));
				}

				// Right 
				else if (mInputHandler->IsKeyDown(OIS::KC_N))
				{
					playAnimation("strafeLeft", time);

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealRight().x, 
						mCamera->mRenderCamera->getRealRight().y, mCamera->mRenderCamera->getRealRight().z); 

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,0,0));
					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * -40, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -40));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * -40,
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -40));
				}

				// Does the rotation counter-clockwise

				if (mInputHandler->IsKeyDown(OIS::KC_LEFT))
				{

					playAnimation("turnLeft", time);

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,1,0));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(0,
						mPhysManager->_world->getGravity().getY() + 70,0));
				} 
				// Does rotation clockwise 
				else if (mInputHandler->IsKeyDown(OIS::KC_RIGHT))
				{
					playAnimation("turnRight", time);

					mPlayerObject->fallRigidBody->setAngularVelocity(btVector3(0,-1,0));
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(0,
						mPhysManager->_world->getGravity().getY() + 70,0));
				}

				// Moves Forward 
				if (mInputHandler->IsKeyDown(OIS::KC_UP))
				{
					playAnimation("walkForward", time);

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
						mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX(), 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()));

					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * 40, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * 40)); 
				}

				// Moves Backward 
				else if (mInputHandler->IsKeyDown(OIS::KC_DOWN))
				{

					playAnimation("walkBackward", time);

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
						mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX() * -1, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()) * -1);
					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * -40, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * -40)); 
				}

				else if (mInputHandler->IsKeyDown(OIS::KC_LCONTROL))
				{
					playAnimation("crouch_idle", time);
				}
				else if (mInputHandler->IsKeyDown(OIS::KC_LSHIFT))
				{
					playAnimation("run", time);

					btVector3 currCameraPos = btVector3(mCamera->mRenderCamera->getRealDirection().x, 
						mCamera->mRenderCamera->getRealDirection().y, mCamera->mRenderCamera->getRealDirection().z); 

					mPlayerObject->fallRigidBody->applyCentralImpulse(btVector3(currCameraPos.getX(), 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ()));

					mPlayerObject->fallRigidBody->setLinearVelocity(btVector3(currCameraPos.getX() * 100, 
						mPhysManager->_world->getGravity().getY() + 70, currCameraPos.getZ() * 100)); 
				}
			}

			checkGround(5000.0f, false); // checks if player is within stage 
			checkGround(5.0f, true); // check if player is on the ground or currently jumping  
		}

		mPlayerObject->synchWithBullet();
#pragma endregion Input controls for keyboard 
	}
}