if rects overlap?

Blitz3D Forums/Blitz3D Programming/if rects overlap?

This is the code for a simple game I am making just to get the hang of types and to learn collisions. Can someone help me with the colisions part. I am working on the part that tests when a bullet hits the rectangle coming down. The wy I have bin trying to write it is
If RectsOverlap (bullet\x,bullet\y,3,20,moveall\x,moveall\y,20,25) Then

The resulting eror is " variable must be a type"

Here is the code
Graphics 640,480

SetBuffer BackBuffer()
SeedRnd (MilliSecs ())
; make random boxes fall and if one reaches the bottom you loose

Print "this is a game by matt anthony"

Type player
Field x
Field y
Field r,g,b
End Type

Type rocks
Field x
Field y
Field r,g,b
Field fallrate ; alien's fall rate
End Type

Type bullet
Field x
Field y
Field r,g,b
Field bulletrate
End Type

Const rocknumber = 5
For a = 1 To rocknumber

rock.rocks = New rocks
rock\x = Rnd(20,620)
rock\y = 0
rock\fallrate = Rnd (-1,1.5) + Rnd(1.3,2.8)
rock\r=Rnd(100,255)
rock\g=Rnd(1,255)
rock\b=Rnd(1,255)

Next

player.player=New player
; Colour:

player\r = 25
player\g = 0
player\b = 200

player\x = 320
player\y = 440
Rect player\x, player\y, 50, 10, 1
Color 255, 255, 255
Text player\x+80, player\y, "Player 1"








WaitKey()
;start of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
While Not KeyHit(1)
;Cls
For moveall.rocks = Each rocks
Delay 10
moveall\y = moveall\y + moveall\fallrate
Color rock\r, rock\g, rock\b

Rect moveall\x, moveall\y, 20, 25,0 ; Draw each invader
If moveall\y > 480 Then atbottom = 1 ; If any are at bottom of screen, end!
If atbottom=1 Then Print "Game over! The earth is in ashes!":Delay 5000:Print "Hope your happy.":Delay 1000 :End

Next

;203=left,205=right
If KeyDown(203) player\x=player\x-15
If KeyDown(205) player\x=player\x+15
Color player\r, player\g, player\b

Rect player\x, player\y, 50, 10, 1
Rect player\x+5, player\y-8,40,8,1
Rect player\x+20, player\y-16,10,8,1

If KeyHit (57)
CreateBullet(player\x,player\y)
EndIf
UpdateBullets()



Flip
Cls

Wend
;End of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM


Function CreateBullet(x,y)
;Create a new bullet
bullets.bullet = New bullet
bullets\x = x+23
bullets\y = y-20

;bullets\bulletrate=-4
End Function

Function UpdateBullets()
For bullet.bullet=Each bullet
bullet\y=bullet\y-5
If bullet\y < 0
Delete bullet
Else
Color 255,200,200
Rect bullet\x,bullet\y,3,20,1
Color 0,0,200
Rect bullet\x,bullet\y+12,3,6,1
Color Rnd (100,255), Rnd (100,255),0
Rect bullet\x,bullet\y+16,3,6,1

EndIf
;RectsOverlap (rect1 X,rect1 Y,rect1 Width,rect1 Height,rect2 X,rect2 Y,rect2 Width,rect2 Height)
;Parameters
;rect1 X = rectangle 1 x location
;rect1 Y = rectangle 1 y location
;rect1 Width = rectangle 1 width
;rect1 Height = rectangle 1 height
;rect2 X = rectangle 2 x location
;rect2 Y = rectangle 2 y location
;rect2 Width = rectangle 2 width
;rect2 Height = rectangle 2 height

If RectsOverlap (bullets\x,y,3,20,moveall\x,moveall\y,20,25) Then
;If RectsOverlap (
;End
EndIf

Next


End Function

--------Changed original Post (I was wrong!)

Okay

From what I see initially, you have called the types 'Bullets' and 'Bullet' - dunno if these should be consistent???

Also, every time you FOR/NEXT the types, I think you lose the association when the loop has completed, so when you wish to use Bullet(s) and Moveall again, there is no type referenced.

I hope this makes sense - Im not too good at explaining it.

In short -

make a new FOR/NEXT loop to check your types for the RectOverlap check









HERES THE WORKING CODE

Graphics 640,480 

SetBuffer BackBuffer() 
SeedRnd (MilliSecs ()) 
; make random boxes fall and if one reaches the bottom you loose 

Print "this is a game by matt anthony" 

Type player 
Field x 
Field y 
Field r,g,b 
End Type 

Type rocks 
Field x 
Field y 
Field r,g,b 
Field fallrate ; alien's fall rate 
End Type 

Type bullet 
Field x 
Field y 
Field r,g,b 
Field bulletrate 
End Type 

Const rocknumber = 5 
For a = 1 To rocknumber 

rock.rocks = New rocks 
rock\x = Rnd(20,620) 
rock\y = 0 
rock\fallrate = Rnd (-1,1.5) + Rnd(1.3,2.8) 
rock\r=Rnd(100,255) 
rock\g=Rnd(1,255) 
rock\b=Rnd(1,255) 

Next 

player.player=New player 
; Colour: 

player\r = 25 
player\g = 0 
player\b = 200 

player\x = 320 
player\y = 440 
Rect player\x, player\y, 50, 10, 1 
Color 255, 255, 255 
Text player\x+80, player\y, "Player 1" 








WaitKey() 
;start of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM 
While Not KeyHit(1) 
;Cls 
For moveall.rocks = Each rocks 
Delay 10 
moveall\y = moveall\y + moveall\fallrate 
Color rock\r, rock\g, rock\b 

Rect moveall\x, moveall\y, 20, 25,0 ; Draw each invader 
If moveall\y > 480 Then atbottom = 1 ; If any are at bottom of screen, end! 
If atbottom=1 Then Print "Game over! The earth is in ashes!":Delay 5000:Print "Hope your happy.":Delay 1000 :End 

Next 

;203=left,205=right 
If KeyDown(203) player\x=player\x-15 
If KeyDown(205) player\x=player\x+15 
Color player\r, player\g, player\b 

Rect player\x, player\y, 50, 10, 1 
Rect player\x+5, player\y-8,40,8,1 
Rect player\x+20, player\y-16,10,8,1 

If KeyHit (57) 
CreateBullet(player\x,player\y) 
EndIf 
UpdateBullets() 



Flip 
Cls 

Wend 
;End of mainMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM 


Function CreateBullet(x,y) 
;Create a new bullet 
bullets.bullet = New bullet 
bullets\x = x+23 
bullets\y = y-20 

;bullets\bulletrate=-4 
End Function 

Function UpdateBullets() 
For bullet.bullet=Each bullet 
bullet\y=bullet\y-5 

Color 255,200,200 
Rect bullet\x,bullet\y,3,20,1 
Color 0,0,200 
Rect bullet\x,bullet\y+12,3,6,1 
Color Rnd (100,255), Rnd (100,255),0 
Rect bullet\x,bullet\y+16,3,6,1 

For moveall2.rocks = Each rocks 
If RectsOverlap (bullet\x,bullet\y,3,20,moveall2\x,moveall2\y,20,25) Then 
;If RectsOverlap ......
;End 
EndIf 

Next

If bullet\y < 0 
Delete bullet
EndIf

Next 


End Function 



Note the changes

Moveall becomes Moveall2 for the collision check
Plus I re-FOR NEXT'ed Moveall to perform the check on each 'rock'
you omitted Bullet\y in the line in question and just put y

Finally, I moved the whole 'Delete bullet' thing, because otherwise the program deletes bullets that go off screen, but then tries to perform collision check on them - giving an OBJECT DOES NOT EXIST ERROR.


The above code now works - quite well too very promising for a simple game!
+++++++++++++++++++++++++++++++++++++++++++++++++++

Hope it helps!

I tried it and got an eror "arithmatic oprator cannot be applied to customtype objects"

That maybe because you have a '/' instead of a '\' when dealing with types, RiverRatt

First I tried (bullet\x,bullet\y,3,20,moveall\x,moveall\y,20,25)
and got an eror but making moveall into moveall2 seams to be working but I will have to work on it some more becouse now I get "memory access violation" I think it will work though. It seems to me that it should be
(bullet\x,bullet\y,3,20,rock\x,rock\y,20,25)
but I would have to make a new for each loop ???

"That maybe because you have a '/' instead of a '\' when dealing with types, RiverRatt"
Oh ya thanks!

hmm.... Did you post the actual code you're working with or have you substituted it for loaded images etc???

Also, what versin of Blitz are you using? I made the changes on Blitz3D v1.85
-

Try again, cos I have been constantly re-editing the code where I noticed other bits ;)

I dont understand your question.
hmm.... Did you post the actual code you're working with or have you substituted it for loaded images etc???
I am writing the code to learn collitions for a 3d game I already have in the works. Models loaded and every thing but all you can do sofar is drive around in a hover craft and look at the pretty stars and lens flares. lots of its code is from the sample folder but changed allot.

I am using Blitz3D v1.85
Try again, cos I have been constantly re-editing the code where I noticed other bits ;) Do you meen mistakes by bits?
please your input is greatly apreceated, even if my spelling is not.


I dont understand your question.
hmm.... Did you post the actual code you're working with or have you substituted it for loaded images etc???



Don't worry- I was just curious. I often try and simplify the mass of code I have problems with when I post on here, to focus on the actual problem.





Try again, cos I have been constantly re-editing the code where I noticed other bits ;) Do you meen mistakes by bits?



Yeah - I kept editing my post where I noticed other changes. but the code sample I posted now should work, though because it works for me. - So I'm not sure how you are getting errors. Have you tried copying what I have written and pasting it into Blitz?

I see I will try it again I must have got it before you were done. thanks

yup it works! Now I'll have to work on what happens when they collide. Thanks for your help. The blitz comunity rules!

No worries!

Good luck - although

I am writing the code to learn collitions for a 3d game I already have in the works.



Collisions work a lot differently in 3D but that's another story!

All the best!

I know, but ya gotta crawl befor you can walk,run ride a bike ,drive a tank, or write a game in c++.
Have a great day!