Skip to main content
GameDev.net gamedev.net
🔒 Locked

Concerning Collision Detection..

Started by kenwi May 23, 2002 at 7:04 PM 0 replies 250+ views
Original Post
kenwi
kenwi
Okay, I''ve got as far as I''m going to wite some simple collision detections, but all they do is giving me pain in the ass. It first seemed simple enough, check if the ball position is lower than one of the sides, if it is then make the ball bounce. I wrote down all the if''s I thought might be usefull.. But there seems like there''s something crucial I''ve missed, the boat left and I wasn''t on the fucking boat! The problem is the following; If I only do a check with ONE of the sides active everythings seems to be just fine. But if I combine the oposite side of that all hell breaks loose. And if I dont include the oposite side, but use the side "Top" and "Left" the ball dosesn''t go in the direction it''s supposed to, instead it goes in the direction it came from in the first place and that''s not what I want! :/ So if anyone out there can hear me lying on my knees begging for help to sort out theese rather lousy if''s I''ve written please, pretty please with pink jelly on the top and sugar help me out. If I am totally lost on the concept with collision detection here do please tell me. Thanks in advance.. And for those who wants to help me, here is the code I''ve written..
    
// Top

if((Ball->position.y - Ball->size) < (Paddle->position.y + Paddle->heigth))
{
if((Ball->position.x - Ball->size) < (Paddle->position.x +   Paddle->width))
Ball->direction = -Ball->direction;

if((Ball->position.x  + Ball->size) < (Paddle->position.x - Paddle->width))
Ball->direction = -Ball->direction;
}

// Bottom

if(( Ball->position.y + Ball->size) > (Paddle->position.y - Paddle->heigth))
{
if((Ball->position.x -Ball->size) < (Paddle->position.x + Paddle->width))
Ball->direction = -Ball->direction;

if((Ball->position.x + Ball->size) < (Paddle->position.x - Paddle->width))
Ball->direction = -Ball->direction;
}

// Right Side

if((Ball->position.x-Ball->size) < (Paddle->position.x+Paddle->width))
{
if((Ball->position.y-Ball->size) < (Paddle->position.y+Paddle->heigth))
Ball->direction = 180-Ball->direction;

if((Ball->position.y - Ball->size) < (Paddle->position.y - Paddle->heigth))
Ball->direction = 180-Ball->direction;
}


// Left Side

if((Ball->position.x + Ball->size) > (Paddle->position.x - Paddle->width))
{

if((Ball->position.y - Ball->size) < (Paddle->position.y + Paddle->heigth))
Ball->direction = 180-Ball->direction;

if((Ball->position.y - Ball->size) < (Paddle->position.y - Paddle->heigth))
Ball->direction = 180-Ball->direction;
}

// Rigth Side

if ((Ball->position.x+Ball->size) < (Paddle->position.x-Paddle->width))
{
if((Ball->position.y-Ball->size) < (Paddle->position.y+Paddle->heigth))Ball->direction= 180-Ball->direction;

if((Ball->position.y-Ball->size) < (Paddle->position.y-Paddle->heigth))
Ball->direction= 180-Ball->direction;
}    
Kenneth Wilhelmsen I''m working on a new 3d engine, this one is going to be opensource and all that.. But first I just want to show the executable off. Try out my Engine
Or just visit my site -------------------------- He who joyfully marches to music in rank and file has already earned my contempt. He has been given a large brain by mistake, since for him the spinal cord would fully suffice. This disgrace to civilization should be done away with at once. Heroism at command, senseless brutality, deplorable love-of-country stance, how violently I hate all this, how despicable and ignoble war is; I would rather be torn to shreds than be a part of so base an action! It is my conviction that killing under the cloak of war is nothing but an act of murder
EbonySeraph
EbonySeraph
I am working on a breakout game just like you...(or maybe you''re doing pong) but here''s my paddle collision detection code:


  
VECTOR vec = ball.LeadingEdge();

if( vec.GetX() >= location.left - 5 && vec.GetX() <= location.right + 5)
{
VECTOR xshift; //Calculate how much to change the ball''s x velocity

xshift.SetY(0); //We don''t want to change the ball''s Y velocity.

int dif = vec.GetX() - (location.left - 5);

if( dif < 12)
xshift.SetX(-5);
else if( dif < 25 )
xshift.SetX(-3);
else if( dif > 63 )
xshift.SetX(6);
else if( dif > 50 )
xshift.SetX(3);
else
xshift.SetX(5);

ball.ReflectUp(xshift);
}



The xshift vector adjusts the x speed of the ball depending on where the ball hits the paddle. This function only checks if there is a collision using the X because before the function is even called, the ball is checked to see if it''s in Y range.

Here is that check if you are curious:


  
//Check if ball hits paddle

for(i = 0; i < ballcount; i++)
{
if( balls[i].LeadingEdge().GetY() < 555 ||
balls[i].LeadingEdge().GetY() > 565) //Further than 8 pixels from the paddle surface

continue; //Check next ball

paddle->Collide(balls[i]);
}


Of course that loops through all balls because in breakout it''s possible to have more than one ball. That if statement is more of, it this ball is out of range, don''t collide, rather than, if it''s in range, collide.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.