Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Collision Detection
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: 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
Sponsor
sponsor
Cervantes




PostPosted: 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




PostPosted: 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 Confused
Cervantes




PostPosted: Sat Sep 24, 2005 6:17 pm   Post subject: (No subject)

Oh my gosh! Thank you for catching that! +allmybits
Mr. T




PostPosted: Sat Sep 24, 2005 6:24 pm   Post subject: Alex's Opinion

Sad i was being serious...whatd u wanna say?
Cervantes




PostPosted: Sat Sep 24, 2005 6:39 pm   Post subject: (No subject)

Between different balls. Hence the circular collision detection approach.
Mr. T




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Mr. T




PostPosted: 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 Confused

[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




PostPosted: 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




PostPosted: Sat Sep 24, 2005 7:53 pm   Post subject: Alex's Opinion

could you explain what the * is supposed to represent?
Mr. T




PostPosted: 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




PostPosted: 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




PostPosted: Sat Sep 24, 2005 8:12 pm   Post subject: Alex's Opinion

Sorry, maybe I was being unclear. Here is my old version of JezzBall.
http://www.compsci.ca/v2/viewtopic.php?t=9749&postdays=0&postorder=asc&start=0
I wanna basically do the same thing, minus the process.
Cervantes




PostPosted: Sun Sep 25, 2005 8:13 am   Post subject: (No subject)

Here's a small example, illustrating how to move and create pseudo-objects simultaneously, without the use of processes.

Turing:

View.Set ("offscreenonly")

var counter := 0

type obj :
    record
        x, y, vx, vy : real
        radius : int
    end record

var ball : flexible array 1 .. 0 of obj

fcn Init_Obj : obj
    var object : obj
    object.radius := Rand.Int (3, 10)
    object.x := Rand.Int (object.radius, maxx - object.radius)
    object.y := Rand.Int (object.radius, maxy - object.radius)
    object.vx := Rand.Int (-3, 3)
    object.vy := Rand.Int (-3, 3)
    result object
end Init_Obj

proc Create_New_Ball
    new ball, upper (ball) + 1
    ball (upper (ball)) := Init_Obj
end Create_New_Ball

loop

    counter := (counter + 1) mod 100
    if counter = 0 then
        Create_New_Ball
    end if

    for i : lower (ball) .. upper (ball)
        if ball (i).x <= ball (i).radius | ball (i).x >= maxx - ball (i).radius then
            ball (i).vx *= -1
        end if
        if ball (i).y <= ball (i).radius | ball (i).y >= maxy - ball (i).radius then
            ball (i).vy *= -1
        end if
        ball (i).x += ball (i).vx
        ball (i).y += ball (i).vy
    end for

    cls
    for i : lower (ball) .. upper (ball)
        Draw.FillOval (round (ball (i).x), round (ball (i).y), ball (i).radius, ball (i).radius, black)
    end for
    View.Update
    delay (10)
end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 29 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: