I'm having trouble with collision detection.
-
- Posts: 4
- Joined: April 23rd, 2012, 6:25 am
I'm having trouble with collision detection.
I'm trying to use the coding skills I learned in the Poo Eating game to make Pong. Why will my collision detection not work?
Please, Please help me figure out how to make the stupid ball do what I want it to do.
Please, Please help me figure out how to make the stupid ball do what I want it to do.
- Attachments
-
- Chili DirectX Framework - Copy.zip
- (156.86 KiB) Downloaded 225 times
Re: I'm having trouble with collision detection.
i'm not quite sure what u're trying to do from looking at ur code but i've found a few problems i wanna go over real quick
from line 132
basicly what u do here makes no sense at all
first u move the ball to the right 2 pixels, and then u check to see if the ball have collided and if true then u move it back 2 pixels ( in other words it stands still )
now the last piece of that code doesn't make any sense when u think about it - basicly what u're checking to see is that if ballX is equal to less than ballX - in other words what u're asking is that if 4 is equal to 2 and that will ofcourse never be true
what I'd do is that I would make a new variable called ballSpeedX or something like that - and then on collision I'd multiply that with -1 which will make the ball go in the opposite direction
something like this
last on from line 116
hope that helps
from line 132
Code: Select all
// Moves ball
ballX = ballX + 2;
if ( ballX + 15 < paddleX + 10 )
{
collide = true;
}
if ( collide == true )
{
ballX = ballX - 2;
}
if ( ballX == ballX - 2 ) // sense - this makes none
{
ballX = ballX - 2;
}
first u move the ball to the right 2 pixels, and then u check to see if the ball have collided and if true then u move it back 2 pixels ( in other words it stands still )
now the last piece of that code doesn't make any sense when u think about it - basicly what u're checking to see is that if ballX is equal to less than ballX - in other words what u're asking is that if 4 is equal to 2 and that will ofcourse never be true
what I'd do is that I would make a new variable called ballSpeedX or something like that - and then on collision I'd multiply that with -1 which will make the ball go in the opposite direction
something like this
Code: Select all
// Moving ball
ballX = ballX + ballSpeedX;
// Checking for collision
if( ball collided with pad ){ // sorry for being lazy here
ballSpeedX = ballSpeedX * -1;
}
last on from line 116
Code: Select all
if ( ballX + 15 > 749 )
{
ballX = 749 - 15; // remember to add -15 when u add it to ballX above or it won't align right
}
Re: I'm having trouble with collision detection.
Edad beat me to it.
There are a number of issues, but I'd say your first and biggest problem is that you need to have a velocity variable for your ball and you need to have that variable negated (flipped opposite) when the ball collides with the paddle. Get that sorted first bro.
There are a number of issues, but I'd say your first and biggest problem is that you need to have a velocity variable for your ball and you need to have that variable negated (flipped opposite) when the ball collides with the paddle. Get that sorted first bro.
Chili
-
- Posts: 4
- Joined: April 23rd, 2012, 6:25 am
Re: I'm having trouble with collision detection.
How about this? The ball doesn't move.
And another thing I'm worried about is when ballX == paddleX then speed get's reversed. but after that like 1 pixel movement, ballX is no longer equal to paddleX, so wouldn't the speed go back to being positive?
And another thing I'm worried about is when ballX == paddleX then speed get's reversed. but after that like 1 pixel movement, ballX is no longer equal to paddleX, so wouldn't the speed go back to being positive?
- Attachments
-
- Chili DirectX Framework - Copy.zip
- (164.19 KiB) Downloaded 201 times
Re: I'm having trouble with collision detection.
it's kinda late here so i'll just real quick point out a few problems i see in ur code and if needed go more in detail later
first of all - i noticed something in ur DrawBall function - and looking at ur other draw function they have the same problem
first of all it makes no sense to feed in x and y into the function when u later just force them to be something else
second u don't want to set y and x to anything else than 0 in this case - as that just means that u'll draw the ball in a completely different place than ballX and ballY, which are they variable u're suppose to control it with
here's what it should look like - this way it'll not be offset from ballX and ballY
here's another issue - it's not really big or anything but it would be smarter if u first updated the position, then ran the clamp to window code and then draw it to the screen - so just some rearrangement
ours.:
smart way - as I said it's not a big issue, but it's better practice
lastly for the move ball code
first of all ur colide bool is not used for anything, u just set it to true and nothing else
what u wanna do is take that out and u want to use > instead of == in this case or else the ball can easily slip right past
u also wanna make sure to set ballX to the same value as the one u're checking - like when u're clamping
something like this
it's kinda late here so i havn't tested this code but as far as i can tell it should work
EDIT.:
about u being worried about the speed getting inverted again - yes that is true - u'll need to make speed a member variable of the game class so it will be it's value will be saved - in other words u need to put in the game.h and so on
first of all - i noticed something in ur DrawBall function - and looking at ur other draw function they have the same problem
Code: Select all
void Game::DrawBall ( int x, int y )
{
for ( y = 220; y <= 235; y++ )
{
for ( x = 20; x <= 35; x++)
{
gfx.PutPixel ( x + ballX, y + ballY, 255, 255, 255);
}
}
}
second u don't want to set y and x to anything else than 0 in this case - as that just means that u'll draw the ball in a completely different place than ballX and ballY, which are they variable u're suppose to control it with
here's what it should look like - this way it'll not be offset from ballX and ballY
Code: Select all
void Game::DrawBall()
{
for ( int y = 0; y <= 15; y++ )
{
for ( int x = 0; x <= 15; x++)
{
gfx.PutPixel ( x + ballX, y + ballY, 255, 255, 255);
}
}
}
here's another issue - it's not really big or anything but it would be smarter if u first updated the position, then ran the clamp to window code and then draw it to the screen - so just some rearrangement
ours.:
Code: Select all
DrawPaddle ( paddleX, paddleY );
// Clamps paddle to screen
if ( paddleX < 5 )
{
paddleX = 5;
}
if ( paddleX > 795 )
{
paddleX = 795;
}
if ( paddleY < 5 )
{
paddleY = 5;
}
if ( paddleY > 595 )
{
paddleY = 595;
}
// Moves Paddle
if ( kbd.UpIsPressed() )
{
paddleY = paddleY - 3;
}
if ( kbd.DownIsPressed() )
{
paddleY = paddleY + 3;
}
Code: Select all
// Moves Paddle
if ( kbd.UpIsPressed() )
{
paddleY = paddleY - 3;
}
if ( kbd.DownIsPressed() )
{
paddleY = paddleY + 3;
}
// Clamps paddle to screen
if ( paddleX < 5 )
{
paddleX = 5;
}
if ( paddleX > 795 )
{
paddleX = 795;
}
if ( paddleY < 5 )
{
paddleY = 5;
}
if ( paddleY > 595 )
{
paddleY = 595;
}
// Draw paddle
DrawPaddle ();
lastly for the move ball code
Code: Select all
// Moves ball
int speed;
speed = 3;
if ( ballX == paddleX )
{
collide = true; // this is not used for anything
speed = speed * -1;
}
ballX = ballX + speed;
}
what u wanna do is take that out and u want to use > instead of == in this case or else the ball can easily slip right past
u also wanna make sure to set ballX to the same value as the one u're checking - like when u're clamping
something like this
Code: Select all
/* move ball code here */
// collide with paddle
if( ballX > paddleX )
{
speed = speed * -1;
ballX = paddleX;
}
/* draw ball code here */
EDIT.:
about u being worried about the speed getting inverted again - yes that is true - u'll need to make speed a member variable of the game class so it will be it's value will be saved - in other words u need to put in the game.h and so on
-
- Posts: 4
- Joined: April 23rd, 2012, 6:25 am
Re: I'm having trouble with collision detection.
This is all I have. I am just completely lost as to how to make this darn program work. If it's too much work to help me, I understand. BUT, I just wanted to say to you guys and chili that this forum and chili's videos are very much appreciated. It's so great that there's a place like this forum for people like us to get together and work together. So thanks for that.
I'll post my code. Mostly, I just don't really understand how collision works. I don't really know why it's ballX + 15 instead of just ballX. stuff like that.
Thanks so much for any help!
I'll post my code. Mostly, I just don't really understand how collision works. I don't really know why it's ballX + 15 instead of just ballX. stuff like that.
Thanks so much for any help!
- Attachments
-
- Chili DirectX Framework - Copy.zip
- (164.47 KiB) Downloaded 208 times
Re: I'm having trouble with collision detection.
jlathrop16: It seems you have a lack of understanding regarding the fundamentals of collision detection. If you have any specific questions I would be glad to answer them.
For example, you asked this:
EdadTace: Thanks for the posts bro. Above and beyond.
For example, you asked this:
ballX is the x-coordinate of the top-left corner of the ball. Therefore, it is located at the left side of the ball. However, it is the right side of the ball which collides with the paddle. Accordingly, we must test whether the x-coordinate of the right side of the ball exceeds the x-coordinate of the left side of the paddle. Since the width of the ball is 15 pixels, the x-coordinate of the right side of the ball is ballX + 15.jlathrop16 wrote:I don't really know why it's ballX + 15 instead of just ballX.
EdadTace: Thanks for the posts bro. Above and beyond.
Chili
-
- Posts: 4
- Joined: April 23rd, 2012, 6:25 am
Re: I'm having trouble with collision detection.
Ok, I get that now. But here's another problem. The ball moves towards the edge of the screen and when it get like an inch from the paddle, it jumps like .5 inches forward and stops before it touches the paddle. It just stays there...
I guess I'm just not ready to make Pong. I'm going to go redo maybe tutorials starting with 8.
You mentioned that you would maybe make a video for doing a small game...It should be pong haha. :p
I guess I'm just not ready to make Pong. I'm going to go redo maybe tutorials starting with 8.
You mentioned that you would maybe make a video for doing a small game...It should be pong haha. :p