Computer Science Canada

Collisions...

Author:  slender0123 [ Mon Sep 09, 2013 6:00 pm ]
Post subject:  Collisions...

Hello there person who is possibly reading this.. I am farely new to turing, i can get a jumping process going and such, so im trying to make a platformer game, kinda like IWBTG (I Wanna Be The Guy) The only problem is, I couldnt do collisions for the life of me! I can somewhat get it, but its not good enough, I want it so that if a circle hits a line ( as so shown in the program below) It would stop moving down and it could jump on the line, ONLY on the line. if it where to go off the line, it would fall back to the begging. And that if it hits the bottom of the line, it just bounces off and goes back down, I think it would probibly be easier with a Draw.FillBox so i might try that, i just need some guidence in my collisions..


Turing:


const Gravity := 2
var x, y := 100
var y_velocity := 0
var keys : array char of boolean
var tiles : array 0..35, 0..35 of int
setscreen ("offscreenonly,graphics:500;500")
var height := 150
var Length := 150


loop
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) and y <= 0 then
        y_velocity := 30
    end if
    if keys (KEY_LEFT_ARROW) and x > 0 then
        x -= 10
    end if
    if keys (KEY_RIGHT_ARROW) and x < maxx then
        x+= 10
    end if
   
        y_velocity -= Gravity 
    y += y_velocity 

    if y < 0 then 
        y := 0
        y_velocity := 0
    end if
   
 
   
    Draw.FillOval(x,y,15,15,blue)
    drawline(50,height,Length,height,blue)
    View.Update
    delay (5)
    cls
end loop


im currently using 4.1.1.

Author:  Raknarg [ Mon Sep 09, 2013 6:42 pm ]
Post subject:  RE:Collisions...

You could do a bunch of complex math to solve it. The easy way is to use the built in function Math.DistancePointLine (xp, yp, x1, y1, x2, y2).

You give the function an x and y coordinate for the point, and then the x and y coordinates of the line. This will give you the distance between the point and the line.

We can use this. Lets say you have a circle that has a radius of 10 pixels. Every frame you could check the distance between the circle and the line. If the distance is equal to or less than 10, that means they have collided.

Author:  slender0123 [ Mon Sep 09, 2013 7:47 pm ]
Post subject:  Re: Collisions...

code:
if Math.DistancePointLine (y,x, 50, 100,150,100) < 15 then
      put "We have detection!"
      end if


Thats what i got so far, but i find the line detection is still a little off. it doesnt go the the far left of the line, it starts maybe a quarter into the line...

Author:  Raknarg [ Mon Sep 09, 2013 7:49 pm ]
Post subject:  RE:Collisions...

Well the first thing you should do is reverse the x and y coordinates, it looks like you got them backwards

Author:  slender0123 [ Mon Sep 09, 2013 7:52 pm ]
Post subject:  RE:Collisions...

OH! Lol!! Now i feel just silly Lol. thanks! Surprised

Author:  Raknarg [ Mon Sep 09, 2013 7:53 pm ]
Post subject:  RE:Collisions...

aha simple mistakes Razz That's one of the most annoyingkind of errors because it's not a syntax error, so it's harder to figure out whats wrong

Author:  slender0123 [ Mon Sep 09, 2013 8:01 pm ]
Post subject:  RE:Collisions...

True that! Now, to figure out how to get it so you can jump ON the line =p

Author:  Raknarg [ Mon Sep 09, 2013 8:03 pm ]
Post subject:  RE:Collisions...

Well if you have a boolean that keeps track of whether he's in the air or not, then you could just set it to false if he collides with a line

Author:  slender0123 [ Mon Sep 09, 2013 8:05 pm ]
Post subject:  RE:Collisions...

OR i could simply just...

code:
if Math.DistancePointLine (x,y, 50, 100,150,100) < 15 then
      y := 100
      y_velocity := 0
      if keys (KEY_UP_ARROW) and y <= 100 then
         y_velocity := 30
      end if
    end if

Smile

Lastly, under the line.. hmm


: