Posted: Sat Sep 24, 2005 3:03 pm Post subject: Collision Detection
Given the following code, can you show me how to do collision detection with the 1) whatdotcolour method 2) regular method.
And can you explain why the ball looks so jittery.
code:
View.Set ("offscreenonly")
const RADIUS : int := 5
const NUM_BALLS : int := 1
var x, y, dx, dy, clr : array 1 .. NUM_BALLS of int
for i : 1 .. NUM_BALLS
x (i) := Rand.Int (RADIUS, maxx - RADIUS)
y (i) := Rand.Int (RADIUS, maxy - RADIUS)
dx (i) := Rand.Int (-3, 3)
dy (i) := Rand.Int (-3, 3)
clr (i) := Rand.Int (1, 15)
end for
loop
for i : 1 .. NUM_BALLS
if x (i) + dx (i) < RADIUS or
x (i) + dx (i) > maxx - RADIUS then
dx (i) := -dx (i)
end if
if y (i) + dy (i) < RADIUS or
y (i) + dy (i) > maxy - RADIUS then
dy (i) := -dy (i)
end if
x (i) := x (i) + dx (i)
y (i) := y (i) + dy (i)
Draw.FillOval (x (i), y (i), RADIUS, RADIUS, red)
Draw.FillBox (300, 200, 200, 300, blue)
end for
View.Update
cls
end loop
Sponsor Sponsor
Cervantes
Posted: Sat Sep 24, 2005 6:03 pm Post subject: (No subject)
1) Don't go there. There are two reasons I say this. First, it's complicated for this sort of thing. Ideally, you would check a grid of pixels in front of each ball with a width equal to that of the ball and a depth equal to the speed (vector addition of x and y components of velocity) of the ball. This is complicated by the fact that the ball can be moving in 24 different directions.
2) Use the distance formula.
code:
if Math.Distance ( x (j), y (j), x (k), y (k) ) < radius (j) + radius (k) then
%Handle Collision
end if
(This is assuming you are checking for collisions between different, not with that blue square you've got in the middle (?) )
On that note, the blue box drawing method should be outside the for loop. There's no point in drawing a box multiple times. (Yes, your for loop only executes once, but that can be changed easily enough.)
Lastly, it doesn't look jittery to me. It actually runs very fast, as there is no delay.
If you are looking to do collisions with the box, use some if statements.
code:
if x (i) + RADIUS >= box_x1 and y (i) + RADIUS >= box_y1 and x (i) - RADIUS <= box_x2 and y (i) - RADIUS <= box_y2 then
% collision
end if
Mr. T
Posted: Sat Sep 24, 2005 6:10 pm Post subject: Alex's Opinion
Cervantes wrote:
(This is assuming you are checking for collisions between different, not with that blue square you've got in the middle (?) )
leave something out
Cervantes
Posted: Sat Sep 24, 2005 6:17 pm Post subject: (No subject)
Oh my gosh! Thank you for catching that! +allmybits
Mr. T
Posted: Sat Sep 24, 2005 6:24 pm Post subject: Alex's Opinion
i was being serious...whatd u wanna say?
Cervantes
Posted: Sat Sep 24, 2005 6:39 pm Post subject: (No subject)
Between different balls. Hence the circular collision detection approach.
Mr. T
Posted: Sat Sep 24, 2005 6:46 pm Post subject: Alex's Opinion
for this method can you break down what each part represents
code:
if Math.Distance ( x (j), y (j), x (k), y (k) ) < radius (j) + radius (k) then
%Handle Collision
end if
for this method, do i havta break up each part into its own if statement? because each side of the box represents a different collision outcome.
code:
if x (i) + RADIUS >= box_x1 and y (i) + RADIUS >= box_y1 and x (i) - RADIUS <= box_x2 and y (i) - RADIUS <= box_y2 then
% collision
end if
Cervantes
Posted: Sat Sep 24, 2005 7:02 pm Post subject: (No subject)
1) If the distance between the centre's of two balls is less than the sum of their radii, a collision occurs.
2) Sort of. You need to split up the x and the y conditions. Hitting the box from the left or right both result in reversing the x velocity. (vx *= -1) Same goes for hitting the box from the top or bottom.
Sponsor Sponsor
Mr. T
Posted: Sat Sep 24, 2005 7:22 pm Post subject: Alex's Opinion
but if i break it up like this
code:
if x (i) + dx(i) = 300 then
dx (i) := -dx (i)
end if
it covers x = 300 on the entire y axis
[EDIT]
ok, i figured out. you just add restrictions to the what y should equal
code:
if x (i) + dx(i) = 300 and y(i)>=200 and y(i)<=300 then
dx (i) := -dx (i)
end if
Cervantes
Posted: Sat Sep 24, 2005 7:49 pm Post subject: Re: Alex's Opinion
Pwned wrote:
code:
if x (i) + dx(i) = 300 and y(i)>=200 and y(i)<=300 then
dx (i) := -dx (i)
end if
Don't use equals. Use >= or <=. Reason being that the ball is moving at 3 pixels/loop iteration. Thus, at one time it's at 298, the next it's at 301, and the condition has never been met, though it should have.
code:
if x (i) + dx (i) >= 200 and x (i) + dx (i) <= 300 and y (i) >= 200 and y (i) <= 300 then
dx (i) *= -1
end if
%Similarly for the y.
Mr. T
Posted: Sat Sep 24, 2005 7:53 pm Post subject: Alex's Opinion
could you explain what the * is supposed to represent?
Mr. T
Posted: Sat Sep 24, 2005 7:57 pm Post subject: Alex's Opinion
Ok, i am now gonna implement wat I have learned here into my jezzball game. I wanna avoid using processes, so do you have any idea how I can make the movement of the balls and the creation of the walls run in unison?
Cervantes
Posted: Sat Sep 24, 2005 8:08 pm Post subject: (No subject)
Put everything on a counter. Or a timer. Really, I don't know how you COULD put these things into processes!
dx *= -1
is the same as
dx := dx * -1
Just as
x += 1
is the same as
x := x + 1
Mr. T
Posted: Sat Sep 24, 2005 8:12 pm Post subject: Alex's Opinion