• Advertisement
Sign in to follow this  

Get started with 'sliding' collision response

This topic is 1112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I've currently got my collision detection and response implemented quite straight forward.

Now I want to implement 'sliding'. What I've learned/ read is that I should basically do the following:

- check for collision detection in predicted new position

- if collision, redo the movement vector with just X movement and check collision

- do the same for just Z movement

 

If X collides, Z doesn't, just move using the Z component als collision response (or the other way around).

Would this be the way to go/ how should I get started, based on what I have?

 

Any input is appreciated.

void CGame::UpdateGameFP()
{
	VECTOR3 movement = VECTOR3(0.0f, 0.0f, 0.0f);

	if(mInput.KeyDown(VkKeyScan('w')&0xFF)) movement.z = 1.0f;											// DOWN
	if(mInput.KeyDown(VkKeyScan('s')&0xFF)) movement.z = -1.0f;											// DOWN
	
	if(mInput.KeyDown(VkKeyScan('a')&0xFF)) movement.x = -1.0f;											// DOWN
	if(mInput.KeyDown(VkKeyScan('d')&0xFF)) movement.x = 1.0f;											// DOWN

	if(mInput.KeyDown(VK_PRIOR)) movement.y = 1.0f;													// DOWN
	if(mInput.KeyDown(VK_NEXT)) movement.y = -1.0f; 												// DOWN

	movement = mPlayer.NormalizeMovement(movement);

	mD3dcam.Move(movement * mTimer.GetDelta());
	mPlayer.SetPosition(mD3dcam.GetPositionV3());
	mD3dscene.mMeshInst[mPlayerMeshInstId].SetWorldPos(mD3dcam.GetPosition());

	// collision with walls/ maze?
	if(PacmanCollidesWall(mPlayer))							// COLLISION: move back the player
	{
		mD3dcam.SetPosition(mD3dcam.GetLastPosition());
		mPlayer.SetPosition(mD3dcam.GetPositionV3());
		mD3dscene.mMeshInst[mPlayerMeshInstId].SetWorldPos(mD3dcam.GetPosition());
	}

	PlayerThroughMazeHole();

	if(mInput.MouseMoved()) mD3dcam.FreeLook(mInput.GetMouseMoveX(), mInput.GetMouseMoveY(), mPlayer.GetLookSpeed());
}

Edited by cozzie

Share this post


Link to post
Share on other sites
Advertisement

I usually just separate the movement for the axises.

player.x += movement.x;
if(map.collides(player)) { /* move back on x-axis */ }

player.y += movement.y;
if(map.collides(player)) { /* move back on y-axis */ }

Share this post


Link to post
Share on other sites

Yeah, that will make the player slide against walls. And instead of moving back to the old position, you should calculate the actual position where it's against the wall. That would fix some problems with high speeds.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement