コード例 #1
0
ファイル: NPC.cpp プロジェクト: pandaforks/Mirage--
void NPC::CheckCollision(float fSeconds, Map& map, Character& character) {
	// Check collision
	SRect bb = GetBoundingBox();
	SRect newbb = bb + SVector2(mVelocity.x, 0.0f);
	SRect rightbb = map.GetBoundingBoxFromSegment(newbb.GetRightSegment(),
			*this, character);
	SRect leftbb = map.GetBoundingBoxFromSegment(newbb.GetLeftSegment(), *this,
			character);

	// Right collision
	if (mVelocity.x > 0.0f && rightbb.IsValid()) {
		mNPCData.mPosition.x += static_cast<int>(rightbb.min.x - bb.max.x)
				- 1.0f;
	}
	// Left collision
	else if (mVelocity.x < 0.0f && leftbb.IsValid()) {
		mNPCData.mPosition.x += static_cast<int>(leftbb.max.x - bb.min.x)
				+ 1.0f;
	} else {
		mNPCData.mPosition.x += static_cast<int>(mVelocity.x);
	}

	// Check collision
	newbb = bb + SVector2(0.0f, mVelocity.y);
	SRect bottombb = map.GetBoundingBoxFromSegment(newbb.GetBottomSegment(),
			*this, character);
	SRect topbb = map.GetBoundingBoxFromSegment(newbb.GetTopSegment(), *this,
			character);

	// Bottom collision
	if (mVelocity.y > 0.0f && bottombb.IsValid()) {
		mNPCData.mPosition.y += static_cast<int>(bottombb.min.y - bb.max.y)
				- 1.0f;
		mVelocity.y = 0.0f;
		Graphics_DebugRect(newbb + sOffset, 0xff00ff);
		Graphics_DebugRect(bottombb + sOffset, 0xff0000);
	}

	// Top collision
	else if (mVelocity.y < 0.0f && topbb.IsValid()) {
		mNPCData.mPosition.y += static_cast<int>(topbb.max.y - bb.min.y) + 1.0f;
		mVelocity.y = 0.0f;
		Graphics_DebugRect(newbb + sOffset, 0xff00ff);
		Graphics_DebugRect(topbb + sOffset, 0xff0000);
	} else {
		mNPCData.mPosition.y += static_cast<int>(mVelocity.y);
	}
}
コード例 #2
0
ファイル: Character.cpp プロジェクト: bretthuff22/Breaker
void Character::Update(float deltaTime, const Map& map)
{
	const float kSpeed = 500.0f;

	//Check horizontal movement
	if (Input_IsKeyDown(Keys::RIGHT))
	{
		mVelocity.x = kSpeed * deltaTime;
	}
	else if (Input_IsKeyDown(Keys::LEFT))
	{
		mVelocity.x = -kSpeed * deltaTime;
	}
	else
	{
		mVelocity.x = 0.0f;
	}

	// Check collision
	SRect bb = GetBoundingBox();
	SRect newbb = bb + SVector2(mVelocity.x, 0.0f);
	SRect rightbb = map.GetBoundingBoxFromSegment(newbb.GetRightSegment());
	SRect leftbb = map.GetBoundingBoxFromSegment(newbb.GetLeftSegment());

	// Right collision
	if (mVelocity.x > 0.0f && rightbb.IsValid())
	{
		mPosition.x += (int)(rightbb.min.x - bb.max.x) - 1.0f;
	}
	// Left collision
	else if (mVelocity.x < 0.0f && leftbb.IsValid())
	{
		mPosition.x += (int)(leftbb.max.x - bb.min.x) + 1.0f;
	}
	else
	{
		mPosition.x += (int)mVelocity.x;
	}


	//Check vertical movement
	//if (Input_IsKeyDown(Keys::DOWN))
	//{
	//	mVelocity.y = kSpeed * deltaTime;
	//}
	//else if (Input_IsKeyDown(Keys::UP))
	//{
	//	mVelocity.y = -kSpeed * deltaTime;
	//}
	//else
	//{
	//	mVelocity.y = 0.0f;
	//}

	if(!mJumping && Input_IsKeyPressed(Keys::UP))
	{
		mVelocity.y = -30.0f;
		mJumping = true;
	}
	else
	{
		mVelocity.y += 100.0f * deltaTime;
	}

	mVelocity.y = Min(mVelocity.y, 30.0f);

	// Check collision
	newbb =  bb + SVector2(0.0f, mVelocity.y);
	SRect bottombb = map.GetBoundingBoxFromSegment(newbb.GetBottomSegment());
	SRect topbb = map.GetBoundingBoxFromSegment(newbb.GetTopSegment());

	// Bottom collision
	if(mVelocity.y > 0.0f && bottombb.IsValid())
	{
		mPosition.y += (int)(bottombb.min.y - bb.max.y) - 1.0f;
		mVelocity.y = 0.0f;
		mJumping = false;
	}
	// Top collision
	else if(mVelocity.y < 0.0f && topbb.IsValid())
	{
		mPosition.y += (int)(topbb.max.y - bb.min.y) + 1.0f;
		mVelocity.y = 0.0f;
	}
	else
	{
		mPosition.y += (int)mVelocity.y;
	}



}
コード例 #3
0
ファイル: Character.cpp プロジェクト: Khillasaurus/School
void Character::Update(float deltaTime, const Map& map, const SVector2& renderOffset)
{
	const float kSpeed = 256.0f;

	// Check horizontal movement
	if(Input_IsKeyDown(Keys::RIGHT))
	{
		mVelocity.x = kSpeed * deltaTime;
		mSprite.SetRotation(1.575f);
	}
	else if(Input_IsKeyDown(Keys::LEFT))
	{
		mVelocity.x = -kSpeed * deltaTime;
		mSprite.SetRotation(-1.575f);
	}
	else
	{
		mVelocity.x = 0.0f;
	}

	// Check collision
	SRect bb = GetBoundingBox(renderOffset);
	SRect newbb = bb + SVector2(mVelocity.x, 0.0f);

	SLineSegment tempRightLS(newbb.GetRightSegment().from.x - renderOffset.x,
							newbb.GetRightSegment().from.y - renderOffset.y,
							newbb.GetRightSegment().to.x - renderOffset.x,
							newbb.GetRightSegment().to.y - renderOffset.y);
	SLineSegment tempLeftLS(newbb.GetLeftSegment().from.x - renderOffset.x,
							newbb.GetLeftSegment().from.y - renderOffset.y,
							newbb.GetLeftSegment().to.x - renderOffset.x,
							newbb.GetLeftSegment().to.y - renderOffset.y);

	SRect rightbb = map.GetBoudingBoxFromSegment(tempRightLS);
	SRect leftbb = map.GetBoudingBoxFromSegment(tempLeftLS);

	Graphics_DebugRect(bb, 0xF04BC3);
	Graphics_DebugRect(newbb, 0x004BC3);
	Graphics_DebugRect(rightbb, 0xF04B00);
	Graphics_DebugRect(leftbb, 0xF000C3);
	
	// Right Collision
	if(mVelocity.x > 0.0f && rightbb.IsValid())
	{
		mPosition.x += static_cast<int>(rightbb.min.x - bb.max.x) - 1.0f;
	}
	// Left Collision
	else if(mVelocity.x < 0.0f && leftbb.IsValid())
	{
		mPosition.x += static_cast<int>(leftbb.max.x - bb.min.x) + 1.0f;
	}
	else
	{
		mPosition.x += static_cast<int>(mVelocity.x);
	}

	// Check vertical movement
	if(Input_IsKeyDown(Keys::DOWN))
	{
		mVelocity.y = kSpeed * deltaTime;
		mSprite.SetRotation(3.15f);
	}
	else if(Input_IsKeyDown(Keys::UP))
	{
		mVelocity.y = -kSpeed * deltaTime;
		mSprite.SetRotation(0.0f);
	}
	else
	{
		mVelocity.y = 0.0f;
	}

	// Check collision
	newbb = bb + SVector2(0.0f, mVelocity.y);
	SRect topbb = map.GetBoudingBoxFromSegment(newbb.GetTopSegment());
	SRect bottombb = map.GetBoudingBoxFromSegment(newbb.GetBottomSegment());
	
	// Top Collision
	if(mVelocity.y < 0.0f && topbb.IsValid())
	{
		mPosition.y += static_cast<int>(topbb.max.y - bb.min.y) + 1.0f;
	}
	// Bottom Collision
	else if(mVelocity.y > 0.0f && bottombb.IsValid())
	{
		mPosition.y += static_cast<int>(bottombb.min.y - bb.max.y) - 1.0f;
	}
	else
	{
		mPosition.y += static_cast<int>(mVelocity.y);
	}

	mPosition += mVelocity;
}