Learning Collisions .. very hard please help
Author |
Message |
KawaNinja
|
Posted: Tue Jan 27, 2004 2:02 pm Post subject: Learning Collisions .. very hard please help |
|
|
Hello ...
I just finished a grade 10 course for programming using turing, and i decided i wanted to explore it more and see how good it really is, so i decided to make a PONG game, it took me awhile get the paddles to move clearly and all but now i need to find out how to make the ball bounce off the paddle once it hits it, i have been looking at these other codes and i found out that it is very confusing and i cant follow it for very long, if someone can please help me and give me a basic code and slowly building up on how to make collisions bounce off a wall or something that would be VERY VERY much appreciated, thanks
Mod Edit: moved, to Turing Help, next time post in the right section |
|
|
|
|
|
Sponsor Sponsor
|
|
|
McKenzie
|
Posted: Tue Jan 27, 2004 2:49 pm Post subject: (No subject) |
|
|
The easiest, and fasted method to check for collisions is with rectangles. So you have 2 rectangles R1 (left1, bott1, right1, top1) and R2 (left2, bott2, right2, top2). They overlap if:
left1 < right2 and right1 > left2 and
bott1 < top2 and top1 > bott2 |
|
|
|
|
|
recneps
|
Posted: Tue Jan 27, 2004 4:52 pm Post subject: (No subject) |
|
|
I would suggest looking at the "Moving House" example (or smooth animation or something in turing examples folder) it shows how they make the house move, and if its x is greater than maxx then dx(the variable that moves it each time) becomes negative x, so it will reverse direction. and so on with y, etc... i suggest looking at my Spencer's Games, Pong in there , itll show you how I did it, and you can adapt it a bit. |
|
|
|
|
|
Cervantes
|
Posted: Tue Jan 27, 2004 5:10 pm Post subject: (No subject) |
|
|
you have four variables
code: | var ball_x, ball_y, ball_dx, ball_dy : int |
ball_dx and ball_dy are the values that make the balls move.
declare them to whatever you want.. This is how to declare them to go to the middle of the screen and go on a 45 degree angle up and right
code: | ball_x := maxx div 2
ball_y := maxy div 2
ball_dx := 1
ball_dy := 1 |
then inside of a loop you need to make the balls actually move
so
code: | loop
ball_x := ball_x + ball_dx
ball_y := ball_y + ball_dy
end loop |
also inside that loop you have to draw the ball
so [somewhere inside the loop, I usually do it near the end]
code: | Draw.FillOval (ball_x, ball_y, 3, 3, brightred) |
the 3's in the Draw.FillOval command represent the ball's x and y radius.
when you want to bounce it, you must do something like
[This is for bouncing it off the left wall]
code: | if ball_x < 3 then
ball_dx := -ball_dx
end if |
to bounce it off a paddle you can use whatdotcolour or you can use an if statement like the one above except that you have to have a x value for the paddle...
code: | if ball_x > paddle_x and ball_x < paddle_x + paddle_width and ball_y < 100 then
ball_dy := -ball_dy
end if |
Hope that helps
Cheers |
|
|
|
|
|
KawaNinja
|
Posted: Tue Jan 27, 2004 7:32 pm Post subject: one problem down.. one more to go :( |
|
|
Thanks so much guys, this has helped me loads on getting a couple steps further on my program.... but i guess the nature of things.. as you progress you run and find more errors, and now i need to find out something that is a bit confusing to explain. Ill try my best though.
Ok, i have a loop and end loop for my paddles (so it can run smooth and can keep on moving and stuff) and i need to have a loop for the ball to move, but when i add in the code for the ball inside the loop of the paddle, it doesnt run right and either the ball goes super fast, or if i put a delay, the paddles move with the delay, so i need to find out how to work both loops at the same time without causing any conflicts.. i tried putting the ball loop under the paddle loop, but it never executes because the paddle loop is in an infinite loop, any ideas to solve this problem ?? sorry if its a bit confusing |
|
|
|
|
|
Cervantes
|
Posted: Tue Jan 27, 2004 7:41 pm Post subject: (No subject) |
|
|
if the ball is moving super fast then you should lower the ball_dx and ball_dy
If that doesn't help post your code and we can fix it up |
|
|
|
|
|
KawaNinja
|
Posted: Tue Jan 27, 2004 8:54 pm Post subject: (No subject) |
|
|
hey.. omg im such a stupid, i spent all day on this for one little stupid stupid error, lmao... i forgot to declare the variables before the loop.. omg what a waste of a day lol thanks anyways..
but now im on to colliding with the paddle, and i cant find out how to get the ball to hit the paddle and bounce back.. i tried to use what you gave me but i didnt get it to work right |
|
|
|
|
|
Thuged_Out_G
|
Posted: Tue Jan 27, 2004 11:28 pm Post subject: (No subject) |
|
|
whatdotcolor would prolly be easiest...its the easiest to understand...basically it is used to check the color of a specified pixel...for example
code: |
if whatdotcolor(ball_x,bally_y)=colorOfPaddle then
ball_dx:=-ball_dx
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Wed Jan 28, 2004 8:45 am Post subject: (No subject) |
|
|
oops sorry kawa, what I gave you for the pong to ball collision works if the paddles are on the top and bottom, which is what I did in my pong game. If your paddles are on left and right sides, then it would be
code: | if ball_y > paddle_y and ball_y < paddle_y + paddle_width and ball_x < 100 then
ball_dx := -ball_dx
end if |
the part about ball_x bein less than 100 just means that its x value is somewhere beyond the paddle.. really you should add another part to the if statement to make it less buggy
code: | if ball_y > paddle_y and ball_y < paddle_y + paddle_width and ball_x < 100 and ball_x > 100 - paddle_height then |
also in this paddle_width would be larger than paddle_height..
paddle_width refers to its biggest side, though that may be confusing, you can just change the variable names if you need to
Cheers |
|
|
|
|
|
|
|