
-----------------------------------
jolly50
Thu Nov 08, 2007 7:52 pm

Game Help - Sprite Collision Detection
-----------------------------------
Hey,

I'm writing a game, but I need help with the collision detection.

My game involves this little football ref that has to avoid and shoot footballs at opposing football running backs

what I would like to do is have it so that when the footballs hit a runningback....that the running back dissappears.

and if the running back hits the ref then its game over.....

so here is what i have so far...


setscreen ("graphics") 
View.Set ("offscreenonly")

var x, y, x1,y1 :int 
var vx, vy: int 
var chars :array char of boolean
var timeLast := Time.Elapsed
var proj : flexible array 1 .. 0 of
    record
        x, y, vx, vy : real
    end record
var playerdir: int:=0

function cooldown (timeDelay : int) : boolean
    if Time.Elapsed - timeLast >= timeDelay then
        timeLast := Time.Elapsed
        result true
    end if
    result false
end cooldown

var pic1:int:= Pic.FileNew ("football_ref.bmp")

x:=25
y:=26
x1:= 400
y1:=26

var spriteman:int
spriteman:=Sprite.New(pic1)

var football:int:=Pic.FileNew("football_ball.bmp")

var jumping: boolean:=false

const gravity:=-1


Sprite.Show (spriteman)

Sprite.SetPosition (spriteman,x,y,false)

procedure controls

Input.KeyDown (chars)

    if chars (KEY_RIGHT_ARROW) then
         x+=2
         playerdir:=0
    end if

    if chars (KEY_LEFT_ARROW) then
        x-=2
        playerdir:=180
    end if

    if chars (KEY_DOWN_ARROW) then
        y-=2
    end if

    if chars (KEY_UP_ARROW) and not jumping and y=26 then
        jumping:=true
        vy:=15
    end if
    
    if chars (' ') then
    end if
    
    if chars (KEY_ENTER) then
        if cooldown (200) then
            new proj, upper (proj) + 1
            if playerdir=0 then
                proj (upper (proj)).x:= x+77
            elsif playerdir=180 then
                proj (upper (proj)).x:= x
            end if
            
            proj (upper (proj)).y:= y+80
            
            if playerdir=0 then
                proj (upper (proj)).vx:=16
            elsif playerdir=180 then
                proj (upper(proj)).vx:=-16
            end if
            
            proj (upper (proj)).vy := 8
            timeLast:= Time.Elapsed
         end if
     end if
           
    if jumping then
        x+=0
        y+=vy
        vy+=gravity
        
        if y 25 then
  x1:=x1-3
end if
    

end computer

loop 
    cls
    drawfillbox (0,0,maxx,30,green)
    Input.KeyDown (chars)
    controls
    updateproj
    computer
    Sprite.SetPosition (spriteman,x,y,false)
    View.Update
    delay (5)
end loop



...sorry its a little messy....and it does have one issue right now..the running back leaves a trail right behind him, but i'm currently working on it...

what should I add to my code to make it work?

-----------------------------------
CodeMonkey2000
Thu Nov 08, 2007 8:37 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
Do you know how a rectangle to rectangle collision works? Just apply that for sprites.

-----------------------------------
Zampano
Thu Nov 08, 2007 8:43 pm

Re: Game Help - Sprite Collision Detection
-----------------------------------
On that note, here is a link to a tutorial on collision detection.
http://compsci.ca/v3/viewtopic.php?t=13661

-----------------------------------
jolly50
Thu Nov 08, 2007 9:21 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
I read the tutorial on collision detection and I'm still a little confused....how would i relate the x1,x2,y1,y2 concept to my sprites.....

-----------------------------------
Zampano
Fri Nov 09, 2007 2:54 am

Re: Game Help - Sprite Collision Detection
-----------------------------------
To the best of my very limited understanding, your sprite should be approximated as a rectangle and the corners of the rectangle should be declared beased on their distance from other corners of the same sprite. I other words, each corner of the sprite picture is a corner used in the collision detection.

I have a sneaking feeling that I am not grasping what you are actually asking though.

-----------------------------------
jolly50
Fri Nov 09, 2007 5:02 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
you sort of got my drift, but what I was really looking for was...

in my code...i have 


Sprite.SetPosition (spriteman,x,y,false)


how would i change that code to put in x1, x2, y1, y2 so that I can use the detection feature

-----------------------------------
Nick
Fri Nov 09, 2007 5:20 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
well x1 wil be x
let y1 be y
that leaves x2 and y2
use Pic.Height and Pic.Width to find x2 and y2
x2=x+Pic.Width(spriteman)
y2=y+Pic.Height(spriteman)

-----------------------------------
jolly50
Fri Nov 09, 2007 9:42 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
ok....lol

I'm confused beyond belief.....

I have no clue what i'm doing....

...can someone help walk me through this?

-----------------------------------
CodeMonkey2000
Fri Nov 09, 2007 9:55 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
Mabey this is too advanced for you?
Think about how x2 relates to x1.

-----------------------------------
jolly50
Fri Nov 09, 2007 10:13 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
ok.....

this might be too advanced...but eventually I have to learn about this....

so...I went into the tutorial about rectangle collision detection and I incorporated my own sprites like this:


setscreen ("graphics")
View.Set ("offscreenonly")


var sprite1_x1:int:= 100
var sprite1_y1:int:= 100
var sprite1_x2:int
var sprite1_y2:int
var sprite2_x1:int:= 200
var sprite2_y1:int:= 100
var sprite2_x2:int
var sprite2_y2:int

var pic1:int:= Pic.FileNew ("football_ref.bmp")
var pic2:int:= Pic.FileNew ("football_ball.bmp")

var spriteman:int
spriteman:=Sprite.New(pic1)
var football:int
football:=Sprite.New(pic2)

Sprite.Show (spriteman)
Sprite.Show (football)

Sprite.SetPosition (spriteman,sprite1_x1,sprite1_y1,false)
Sprite.SetPosition (football,sprite2_x1,sprite2_y1,false)

sprite1_x2:=sprite1_x1+Pic.Width(pic1)
sprite1_y2:=sprite1_y1+Pic.Height(pic1)

sprite2_x2:=sprite2_x1+Pic.Width(pic2)
sprite2_y2:=sprite2_y1+Pic.Height(pic2)


loop
    cls
    delay (20)
    sprite1_x1:=sprite1_x1+1
    if sprite1_x2 > sprite2_x1 and sprite1_x1 < sprite2_x2 and sprite1_y2 > sprite2_y1 and sprite1_y1 < sprite2_y2 then
        put "Sack!"
        exit
    end if
    View.Update
end loop




First, am i going in the right direction?

Second, when I try to run the program...my sprites don't move....

What do I have to do to make my code work?

-----------------------------------
Nick
Fri Nov 09, 2007 10:36 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
redeclare x2 and y2 in ur main loop


loop

    cls
    delay (20)
    sprite1_x1:=sprite1_x1+1
    sprite1_x2:=sprite1_x1+Pic.Width(pic1)
    sprite1_y2:=sprite1_y1+Pic.Height(pic1)

    sprite2_x2:=sprite2_x1+Pic.Width(pic2)
    sprite2_y2:=sprite2_y1+Pic.Height(pic2)

    if sprite1_x2 > sprite2_x1 and sprite1_x1 < sprite2_x2 and sprite1_y2 > sprite2_y1 and sprite1_y1 < sprite2_y2 then
        put "Sack!"
        exit
    end if
    View.Update
end loop 


alse the sprite module does not work on some versions of turing what version are u running?

-----------------------------------
jolly50
Sat Nov 10, 2007 7:17 am

RE:Game Help - Sprite Collision Detection
-----------------------------------
I tried your code and it does say "sack!", but the sprites don't move!?!?!?!?

... and I'm running 4.1

-----------------------------------
jolly50
Sat Nov 10, 2007 11:41 am

RE:Game Help - Sprite Collision Detection
-----------------------------------
...also...I tried to fix my Game's code and this is what I got...


setscreen ("graphics") 
View.Set ("offscreenonly")

var x, y, x1,y1,x2,y2 :int 
var vx, vy: int 
var chars :array char of boolean
var timeLast := Time.Elapsed
var proj : flexible array 1 .. 0 of
    record
        x, y, vx, vy : real
    end record
var playerdir: int:=0



function cooldown (timeDelay : int) : boolean
    if Time.Elapsed - timeLast >= timeDelay then
        timeLast := Time.Elapsed
        result true
    end if
    result false
end cooldown

var pic1:int:= Pic.FileNew ("football_ref.bmp")

x:=25
y:=26
x1:= 400
y1:=26

var spriteman:int
spriteman:=Sprite.New(pic1)

x2:=x+Pic.Width(pic1)
y2:=y+Pic.Height(pic1)

var football:int:=Pic.FileNew("football_ball.bmp")

var pic2:int:= Pic.FileNew ("football_player.bmp")

var footballman:int
footballman:=Sprite.New(pic2)

Sprite.Show (footballman)

Sprite.SetPosition (footballman,x1,y1,false)


var jumping: boolean:=false

const gravity:=-1


Sprite.Show (spriteman)

Sprite.SetPosition (spriteman,x,y,false)

procedure controls

Input.KeyDown (chars)

    if chars (KEY_RIGHT_ARROW) then
         x+=2
         playerdir:=0
    end if

    if chars (KEY_LEFT_ARROW) then
        x-=2
        playerdir:=180
    end if

    if chars (KEY_DOWN_ARROW) then
        y-=2
    end if

    if chars (KEY_UP_ARROW) and not jumping and y=26 then
        jumping:=true
        vy:=20
    end if
    
    if chars (' ') then
    end if
    
    if chars (' ') then
        if cooldown (200) then
            new proj, upper (proj) + 1
            if playerdir=0 then
                proj (upper (proj)).x:= x+77
            elsif playerdir=180 then
                proj (upper (proj)).x:= x
            end if
            
            proj (upper (proj)).y:= y+80
            
            if playerdir=0 then
                proj (upper (proj)).vx:=16
            elsif playerdir=180 then
                proj (upper(proj)).vx:=-16
            end if
            
            proj (upper (proj)).vy := 8
            timeLast:= Time.Elapsed
         end if
     end if
           
    if jumping then
        x+=0
        y+=vy
        vy+=gravity
        
        if y 25 or x x1 and x1 < x2 and y2 > y1 and y1 < y2 then
            put "Game Over"
            exit
        end if 
    View.Update
    delay (10)
end loop



What is wrong with my collison detection's "if statement.....because the game ends when the running back touches the original x coordinate... instead of the new coordinate

-----------------------------------
CodeMonkey2000
Sat Nov 10, 2007 12:06 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
How are you able to use the sprite module? And if you are going to copy/steal code at least give credit.

-----------------------------------
jolly50
Sat Nov 10, 2007 6:30 pm

RE:Game Help - Sprite Collision Detection
-----------------------------------
ummm...I never copy or stole any code!!!!!...Even if I did I would give credit.....I NEVER COPIED OR STOLE ANY CODE...I wrote this entire code with the help of some of my friends in my comp. eng. class
