Hello,
I've made my own collision system (only for squares and rectangles) but I have 2 little bugs that I don't know how to fix
1. sometimes it will pass through texture, but max of 1 or 2 pixels // I've tried to fix this by adding 1 pixel for "bonus collision range" but sometimes it will collide even when they aren't touching...
2. when it collide with something you can move only to 2 sides, for example:
when I collide with right side with player (left side of second object) I can;t move down, only up and left (on the right side is second object)
here's screenshot of first problem (object is locared at 100,100), I've collided at x = 102 which should be 100
http://filebeam.com/b7d18239c8bb509c5747b20c0e3ec46e.jpghere's my code:
Collision function:
bool Animation::IsAt(Animation Collider, char direction)
{
int AnimW, AnimH, ColliderW, ColliderH;
float AnimX, AnimY, ColliderX, ColliderY;
AnimX = Animation::GetX();
AnimY = Animation::GetY();
ColliderX = Collider.GetX();
ColliderY = Collider.GetY();
AnimW = Animation::GetW();
AnimH = Animation::GetH();
ColliderW = Collider.GetW();
ColliderH = Collider.GetH();
if(direction == 'r')
{
if(
(AnimX+AnimW >= ColliderX && AnimX+AnimW <= ColliderX+ColliderW && AnimY >= ColliderY && AnimY <= ColliderY+ColliderH) ||
(AnimX+AnimW >= ColliderX && AnimX+AnimW <= ColliderX+ColliderW && AnimY+AnimH >= ColliderY && AnimY+AnimH <= ColliderY+ColliderH) ||
(ColliderX >= AnimX && ColliderX <= AnimX+AnimW && ColliderY >= AnimY && ColliderY <= AnimY+AnimH) ||
(ColliderX >= AnimX && ColliderX <= AnimX+AnimW && ColliderY+ColliderH >= AnimY && ColliderY+ColliderH <= AnimY+AnimH)
)
{
return true;
}
}
else if(direction == 'l')
{
if(
(AnimX >= ColliderX && AnimX <= ColliderX+ColliderW && AnimY >= ColliderY && AnimY <= ColliderY+ColliderH) ||
(AnimX >= ColliderX && AnimX <= ColliderX+ColliderW && AnimY+AnimH >= ColliderY && AnimY+AnimH <= ColliderY+ColliderH) ||
(ColliderX+ColliderW >= AnimX && ColliderX+ColliderW <= AnimX+AnimW && ColliderY >= AnimY && ColliderY <= AnimY+AnimH) ||
(ColliderX+ColliderW >= AnimX && ColliderX+ColliderW <= AnimX+AnimW && ColliderY+ColliderH >= AnimY && ColliderY+ColliderH <= AnimY+AnimH)
)
{
return true;
}
}
else if(direction == 'u')
{
if(
(AnimX >= ColliderX && AnimX <= ColliderX+ColliderW && AnimY >= ColliderY && AnimY <= ColliderY+ColliderH) ||
(AnimX+AnimW >= ColliderX && AnimX+AnimW <= ColliderX+ColliderW && AnimY >= ColliderY && AnimY <= ColliderY+ColliderH) ||
(ColliderX >= AnimX && ColliderX <= AnimX+AnimW && ColliderY+ColliderH >= AnimY && ColliderY+ColliderH <= AnimY+AnimH) ||
(ColliderX+ColliderW >= AnimX && ColliderX+ColliderW <= AnimX+AnimW && ColliderY+ColliderH >= AnimY && ColliderY+ColliderH <= AnimY+AnimH)
)
{
return true;
}
}
else if(direction == 'd')
{
if(
(AnimX >= ColliderX && AnimX <= ColliderX+ColliderW && AnimY+AnimH >= ColliderY && AnimY+AnimH <= ColliderY+ColliderH) ||
(AnimX+AnimW >= ColliderX && AnimX+AnimW <= ColliderX+ColliderW && AnimY+AnimH >= ColliderY && AnimY+AnimH <= ColliderY+ColliderH) ||
(ColliderX >= AnimX && ColliderX <= AnimX+AnimW && ColliderY >= AnimY && ColliderY <= AnimY+AnimH) ||
(ColliderX+ColliderW >= AnimX && ColliderX+ColliderW <= AnimX+AnimW && ColliderY >= AnimY && ColliderY <= AnimY+AnimH)
)
{
return true;
}
}
return false;
}
how am I detecting collision in game:
if(right)
{
if(!aPlayer[0].IsAt(aBuilding[0], 'r'))
aPlayer[0].Move(Vector2f(Speed*ElapsedTime, 0), 'r');
lastdir = 0;
}
if(left)
{
if(!aPlayer[0].IsAt(aBuilding[0], 'l'))
aPlayer[0].Move(Vector2f(-Speed*ElapsedTime, 0), 'l');
lastdir = 3;
}
if(up)
{
if(!aPlayer[0].IsAt(aBuilding[0], 'u'))
aPlayer[0].Move(Vector2f(0, -Speed*ElapsedTime), 'u');
lastdir = 6;
}
if(down)
{
if(!aPlayer[0].IsAt(aBuilding[0], 'd'))
aPlayer[0].Move(Vector2f(0, Speed*ElapsedTime), 'd');
lastdir = 9;
}
I've been working on this second day, total of like 10 hours and I have no more ideas on how to fix that.. my brain is completelly exhausted ...