| Game Help - Sprite Collision Detection 
 
	 
	
		| Author | Message |   
		| jolly50 
 
  
 
 
 | 
			
				|  Posted: Thu Nov 08, 2007 7:52 pm    Post subject: 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...
 
 
 	  | code: |  	  | 
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 <= 26 then
 jumping:=false
 y:=26
 end if
 end if
 if chars (KEY_CTRL) then
 drawfilloval (x+40,y+30,5,5,7)
 x:=x-5
 end if
 
 if chars (KEY_SHIFT) then
 drawfilloval (x+40,y+30,5,5,7)
 x:=x+5
 end if
 
 if y<26 then
 y:=26
 end if
 
 if x>580 then
 x:=580
 end if
 
 if x<0 then
 x:=0
 
 end if
 
 
 Sprite.SetPosition (spriteman,x,y,false)
 end controls
 
 procedure updateproj
 
 for i : 1 .. upper (proj)
 proj (i).vy += gravity
 proj (i).x += proj (i).vx
 proj (i).y += proj (i).vy
 end for
 
 for i : 1 .. upper (proj)
 if proj (i).x > maxx or proj (i).y < 0 or proj (i).x < 0 then
 proj (i) := proj (upper (proj))
 new proj, upper (proj) - 1
 exit
 end if
 end for
 for i : 1 .. upper (proj)
 Pic.Draw (football,round (proj (i).x), round (proj (i).y),picCopy)
 end for
 end updateproj
 
 procedure computer
 
 
 var pic2:int:= Pic.FileNew ("football_player.bmp")
 
 var footballman:int
 footballman:=Sprite.New(pic2)
 
 Sprite.Show (footballman)
 
 Sprite.SetPosition (footballman,x1,y1,false)
 
 
 if x>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?
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| CodeMonkey2000 
 
 
 
 
 | 
			
				|  Posted: Thu Nov 08, 2007 8:37 pm    Post subject: RE:Game Help - Sprite Collision Detection |  |   
				| 
 |  
				| Do you know how a rectangle to rectangle collision works? Just apply that for sprites. |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Zampano 
 
  
 
 
 |  |   
		|  |  |  
	  
		|  |   
		| jolly50 
 
  
 
 
 | 
			
				|  Posted: Thu Nov 08, 2007 9:21 pm    Post subject: 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 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 2:54 am    Post subject: 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 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 5:02 pm    Post subject: 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
 
 
 	  | code: |  	  | 
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 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 5:20 pm    Post subject: 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 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 9:42 pm    Post subject: 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?
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| Sponsor Sponsor
 
  
   |  |   
		|  |   
		| CodeMonkey2000 
 
 
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 9:55 pm    Post subject: RE:Game Help - Sprite Collision Detection |  |   
				| 
 |  
				| Mabey this is too advanced for you? Think about how x2 relates to x1.
 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		| jolly50 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 10:13 pm    Post subject: 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:
 
 
 	  | code: |  	  | 
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 
 
  
 
 
 | 
			
				|  Posted: Fri Nov 09, 2007 10:36 pm    Post subject: RE:Game Help - Sprite Collision Detection |  |   
				| 
 |  
				| redeclare x2 and y2 in ur main loop 
 	  | Turing: |  	  | 
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 
 
  
 
 
 | 
			
				|  Posted: Sat Nov 10, 2007 7:17 am    Post subject: 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 
 
  
 
 
 | 
			
				|  Posted: Sat Nov 10, 2007 11:41 am    Post subject: RE:Game Help - Sprite Collision Detection |  |   
				| 
 |  
				| ...also...I tried to fix my Game's code and this is what I got... 
 
 	  | code: |  	  | 
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 <= 26 then
 jumping:=false
 y:=26
 end if
 end if
 if chars (KEY_CTRL) then
 drawfilloval (x+40,y+30,5,5,7)
 x:=x-5
 end if
 
 if chars (KEY_SHIFT) then
 drawfilloval (x+40,y+30,5,5,7)
 x:=x+5
 end if
 
 if y<26 then
 y:=26
 end if
 
 if x>580 then
 x:=580
 end if
 
 if x<0 then
 x:=0
 
 end if
 
 
 
 
 Sprite.SetPosition (spriteman,x,y,false)
 end controls
 
 procedure updateproj
 
 for i : 1 .. upper (proj)
 proj (i).vy += gravity
 proj (i).x += proj (i).vx
 proj (i).y += proj (i).vy
 end for
 
 for i : 1 .. upper (proj)
 if proj (i).x > maxx or proj (i).y < 0 or proj (i).x < 0 then
 proj (i) := proj (upper (proj))
 new proj, upper (proj) - 1
 exit
 end if
 end for
 for i : 1 .. upper (proj)
 Pic.Draw (football,round (proj (i).x), round (proj (i).y),picCopy)
 end for
 
 end updateproj
 
 procedure computer
 
 
 
 
 if x>25 or x<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)
 Sprite.SetPosition (footballman,x1,y1,false)
 if  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 
 
 
 
 
 | 
			
				|  Posted: Sat Nov 10, 2007 12:06 pm    Post subject: 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 
 
  
 
 
 | 
			
				|  Posted: Sat Nov 10, 2007 6:30 pm    Post subject: 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 |  
				|  |  |   
		|  |  |  
	  
		|  |   
		|  |  
 |