Computer Science Canada

I need help with collision detection!

Author:  brycemk [ Fri Nov 30, 2012 7:22 pm ]
Post subject:  I need help with collision detection!

Help... any ideas?
<Replace all the <> with your answers/code and remove the <>>


Need help with collision detection
<Answer Here>


I cant seem to get make the collision detection with the falling wall
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


setscreen ("graphics:v16,nocursor,noecho")
randomize
var x1, x2, x3, x4, y1, y2, barspeed, score : int
var c : int
var guy : int := Pic.FileNew ("guy.bmp")
var width : int := Pic.Width (guy)
var height : int := Pic.Height (guy)
var chars : array char of boolean
var x_pos , y_pos : int

x_pos := 300
y_pos :=1
randint (x2, 101, maxx - 151)
colourback (0)
score := 0
x1 := 100
x3 := x2 + 50
x4 := maxx - 100
y1 := maxy
y2 := y1 - 30

barspeed := 7
c := 1


procedure everythingelse
    drawfillbox (0, 0, x1 - 1, maxy, 24)
    drawfillbox (x4 + 1, 0, maxx, maxy, 24)
    drawfillbox (x1, y1, x2, y2, 0)
    drawfillbox (x3, y1, x4, y2, 0)
    y1 -= barspeed
    y2 -= barspeed
    drawfillbox (x1, y1, x2, y2, 24) %left side of thing
    drawfillbox (x3, y1, x4, y2, 24) %right side of thing
    sound (c, 100)
    if y1 < 10 and barspeed < 15 then
        c -= 20
        sound (c, 100)
    elsif barspeed > 15 then
        c += 0
        sound (c, 100)
    else
        c += 1
        sound (c, 100)
    end if
    if y1 < 0 then
        y1 := maxy
        y2 := y1 - 30
        randint (x2, 101, maxx - 151)
        x3 := x2 + 50
        score += barspeed
        if barspeed > 20 then
            barspeed += 0
        else
            barspeed += 1
        end if
    end if
end everythingelse
delay (2000)

loop
Input.KeyDown (chars)
if chars (KEY_LEFT_ARROW) then
   x_pos := max (x_pos - 10, 20)
   width := max (width - 10, 20)
end if
if chars (KEY_RIGHT_ARROW) then
   x_pos := min (x_pos + 10, 600)
   width := min (width + 10, 600)
 end if
cls
 Pic.Draw (guy, x_pos, y_pos,picMerge)

 
delay (1)

if whatdotcolor (x_pos, y_pos) not=0 then

exit
end if
    everythingelse
   
   
end loop

cls
put "Your score is: ", score



Newest version
<Answer Here>

Author:  Panphobia [ Fri Nov 30, 2012 7:31 pm ]
Post subject:  RE:I need help with collision detection!

(x1+x2)^2-(y1+y2)==0, for the edges etc, it checks if the two points you want to collide are occupying the same space

Author:  Dreadnought [ Fri Nov 30, 2012 8:08 pm ]
Post subject:  Re: I need help with collision detection!

Panphobia wrote:

(x1+x2)^2-(y1+y2)==0, for the edges etc, it checks if the two points you want to collide are occupying the same space

You might want to double check your formula.

@brycemk Please fill in the form, it's there for a reason.

What are you trying to do? What exactly is giving you trouble? How have you tried approaching the problem thus far?

Author:  Zren [ Fri Nov 30, 2012 8:26 pm ]
Post subject:  Re: RE:I need help with collision detection!

Panphobia @ Fri Nov 30, 2012 8:31 pm wrote:
(x1+x2)^2-(y1+y2)==0, for the edges etc, it checks if the two points you want to collide are occupying the same space


Um. I'm thinking that you're mixing that formula up. I noticed you wrote it wrong in the other thread as well.

How To Get The Distance Between Two Points (2D)
Or How Math.Distance() Works.


Pythagoras Theorem (which is what circle collision detection is based off) is: a^2 + b^2 = c^2
Which is where we apply it onto the cartesian plane like so: x^2 + y^2 = d^2 [Where d is the distance from origin to point at (x, y). In other words, the hypotenuse.]
Which can be rewritten to be: d = sqrt(x^2 + y^2) [Note that we only take the positive output of sqrt() as a negative distance doesn't make sense.]
Which can be written as f(x, y) = sqrt(x^2 + y^2)

We can use this to get the distance from point A -> point B by first centering all vectors/coordinates to be relative to point A as if it were the origin.
Take:
point_a = (x1, y1)
point_b = (x2, y2)


Which we then translate so that point_a is at origin.
point_a = (x1 - x1, y1 - y1)
point_b = (x2 - x1, y2 - y1)


Which ends up as:
point_a = (0, 0) [Origin]
point_b = (x2 - x1, y2 - y1)


Which we then use in our function:
f(x2 - x1, y2 - y1)
= sqrt((x2 - x1)^2 + (y2 - y1)^2)


~

Edit: So like, making it look pretty took way longer than I thought...

Author:  Panphobia [ Fri Nov 30, 2012 8:32 pm ]
Post subject:  RE:I need help with collision detection!

yea ^

Author:  Panphobia [ Fri Nov 30, 2012 8:34 pm ]
Post subject:  RE:I need help with collision detection!

sorry forgot square root haha Very Happy, pythagorean theorum haha ;D

Author:  Zren [ Fri Nov 30, 2012 8:38 pm ]
Post subject:  Re: RE:I need help with collision detection!

Panphobia @ Fri Nov 30, 2012 9:34 pm wrote:
sorry forgot square root haha Very Happy, pythagorean theorum haha ;D


You were also adding instead of subtracting.

Author:  Panphobia [ Fri Nov 30, 2012 8:43 pm ]
Post subject:  RE:I need help with collision detection!

aha i mixed it all up haha, Razz ((x1-x2)^2+(y1-y2)^2)^1/2

Author:  Panphobia [ Fri Nov 30, 2012 9:54 pm ]
Post subject:  RE:I need help with collision detection!

technically speaking, the square root of 0 is still 0, so you do not need to calculate it Razz, so he does not have to put the square root


: