Advertisement

Concerning Collision Detection..

Started by May 23, 2002 06:46 PM
0 comments, last by kenwi 22 years, 9 months ago
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
The best I can think of is this:

Start working with vectors. Give your ball a velocity vector. A velocity vector starts at the current position of the ball and ends where the ball will be a certain time from now (like 5 milliseconds or whatever).

Go to NeHe's tuts and look up the tutorial on collision detection. It uses plane-sphere collision detection (paddle-ball or wall-ball for you!).

If you detect a collision between the paddle (or wall) and the ball, change the velocity vector accordingly. See the tut how to do this.

For more info on collision detection: Go through the articles on gamedev.net. There's loads and loads of info on sphere-sphere and plane-sphere collision detection as well as vectors and how to use them.

- An eye for an eye will make the world go blind -

[edited by - smarechal on May 24, 2002 4:28:30 AM]

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

This topic is closed to new replies.

Advertisement