Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Collision detection is not accurate


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 optimisez   Members   -  Reputation: 103

Like
0Likes
Like

Posted 28 June 2013 - 09:10 AM

http://i453.photobucket.com/albums/qq253/ulti-killer/13-4.png~original

 

After trying and trying, I still cannot understand why the leg of character exceeds the wall but no clipping issue when I hit the wall from below. How should I fix it to make him standstill on the wall?

void initPlayer()
{
	//	Create texture.
	hr = D3DXCreateTextureFromFileEx(d3dDevice, "player.png", 169, 44, 
		D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
		D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 
		NULL, NULL, &player);

	playerRect.left = playerRect.top = 0;
	playerRect.right = 29; 
	playerRect.bottom = 36;

	playerDest.X  = 0; 
	playerDest.Y  = 564;

	playerDest.length = playerRect.right - playerRect.left;
	playerDest.height = playerRect.bottom - playerRect.top;
}
void initBox()
{
	hr = D3DXCreateTextureFromFileEx(d3dDevice, "brock.png", 330, 132, 
		D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
		D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), 
		NULL, NULL, &box);

	boxRect.left = 33;
	boxRect.top = 0;
	boxRect.right = 63; 
	boxRect.bottom = 30;

	boxDest.X = boxDest.Y = 300;

	boxDest.length = boxRect.right - boxRect.left;
	boxDest.height = boxRect.bottom - boxRect.top;
}
bool spriteCollide(Entity player, Entity target)
{
	float left1, left2;
	float right1, right2;
	float top1, top2;
	float bottom1, bottom2;

	left1 = player.X;
	left2 = target.X;
	right1 = player.X + player.length;
	right2 = target.X + target.length;

	top1 = player.Y;
	top2 = target.Y;

	bottom1 = player.Y + player.height;
	bottom2 = target.Y + target.height;

	if (bottom1 < top2) return false;
	if (top1 > bottom2) return false;

	if (right1 < left2) return false;
	if (left1 > right2) return false;

	return true;
}

void collideWithBox()
{
	if ( spriteCollide(playerDest, boxDest) && keyArr[VK_UP])
		//playerDest.Y += 50;
		playerDest.Y = boxDest.Y + boxDest.height;
	else if ( spriteCollide(playerDest, boxDest) && !keyArr[VK_UP])
		playerDest.Y = boxDest.Y - boxDest.height;
}


Sponsor:

#2 nhatkthanh   Members   -  Reputation: 334

Like
0Likes
Like

Posted 28 June 2013 - 10:56 AM

What are the order of your process inputs, update collision and draw?


Edited by nhatkthanh, 28 June 2013 - 10:56 AM.


#3 optimisez   Members   -  Reputation: 103

Like
0Likes
Like

Posted 28 June 2013 - 11:06 AM

I think is inputs ->  update collision -> draw

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nShowCmd)
{	
	createWindow();
	init();

	while(windowIsRunning())
	{
		update();
		render();
	}
	
	cleanUp();
	
	return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	escToExit(message, wParam);
	playerInput(message, wParam);

	switch(message)
	{
	// Destroy window
	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS