SUPER TIP : image perfect collide with the 4 sides of a square (test)

BlitzPlus Forums/BlitzPlus Programming/SUPER TIP : image perfect collide with the 4 sides of a square (test)

Hi,

I would like to put very easy tips in the code archive. With this first one, you can make a perfect collision with the 4 sides of a squre ( nice for plateformers, etc ). Please, run this code and tell me if that's ok, and I'll put it in the code archive.


;nom de l'application
AppTitle "Collision avec les quatre cotes d'un carre V1.0"
;initialisation du mode graphique
Graphics 640,480,16

;creation de l'image #01
CreateImage01X#=128
CreateImage01Y#=64
CreateImage01=CreateImage(CreateImage01X,CreateImage01Y)
;creation de l'image #02
CreateImage02X#=64
CreateImage02Y#=128
CreateImage02=CreateImage(CreateImage02X,CreateImage02Y)

;variables de l'image #01
Image01X#=0;image 01 X
Image01Y#=0;image 01 Y
Image01Xtampon#=0;image 01 X tampon
Image01Ytampon#=0;image 01 Y tampon
Image01Xvelocite#=0;velocite Image 01 X
Image01Yvelocite#=0;velocite Image 01 Y

;variables de l'image #02
Image02X#=288;image 02 X
Image02Y#=208;image 02 Y

;extra
ExtraEndIf%=0;pour aller sur le pointeur

;*****************
;* boucle du jeu *
;*****************

;initialise le tampon secondaire
SetBuffer BackBuffer()

;tant que "echappe" n'a pas ete presse
While Not KeyDown(1)

;/////////////////////////////////////////////////////////////////////////
;couleur du rafraichissement du tampon secondaire
ClsColor 255,0,0

;efface le tampon secondaire
Cls

;/////////////////////////////////////////////////////////////////////////
;calcul de la velocite Image 01
Image01X=(Image01X+Image01Xvelocite)
Image01Y=(Image01Y+Image01Yvelocite)

;/////////////////////////////////////////////////////////////////////////
;test de la collision
If ImagesOverlap (CreateImage01,Image01X,Image01Y,CreateImage02,Image02X,Image02Y)
	;collision haut
	If (Image01Ytampon+CreateImage01Y-1)<Image02Y
		Image01Y=(Image02Y-CreateImage01Y)
		ExtraEndIf=1
	EndIf
	If ExtraEndIf=1 Goto Saut
	
	;collision bas
	If (Image01Ytampon+1)>(Image02Y+CreateImage02Y)
		Image01Y=(Image02Y+CreateImage02Y)
		ExtraEndIf=1
	EndIf
	If ExtraEndIf=1 Goto Saut
	
	;collision gauche
	If (Image01Xtampon+CreateImage01X-1)<Image02X
		Image01X=(Image02X-CreateImage01X)
		ExtraEndIf=1
	EndIf
	If ExtraEndIf=1 Goto Saut
	
	;collision droite
	If (Image01Xtampon+CreateImage01X+1)>(Image02X+CreateImage02X)
		Image01X=(Image02X+CreateImage02X)
	EndIf
	
	;pointeur
	.Saut
EndIf
;/////////////////////////////////////////////////////////////////////////
;bloc controle par le joueur
DrawBlock CreateImage01,Image01X,Image01Y

;bloc statique
DrawBlock CreateImage02,Image02X,Image02Y
;/////////////////////////////////////////////////////////////////////////
;affiche les textes
Text 0,00,"Informations"
Text 0,20,"Image01X "+Image01X
Text 0,30,"Image01Y "+Image01Y

Text 0,50,"Image01Xvelocite "+Image01Xvelocite
Text 0,60,"Image01Yvelocite "+Image01Yvelocite

;/////////////////////////////////////////////////////////////////////////
;mise a jour
Image01Xtampon=Image01X
Image01Ytampon=Image01Y

ExtraEndIf=0

Image01Xvelocite=0
Image01Yvelocite=0

;/////////////////////////////////////////////////////////////////////////
;basculement des tampons
Flip

;/////////////////////////////////////////////////////////////////////////
;clavier haut
If KeyDown(200) Image01Yvelocite=(-2)
;clavier bas
If KeyDown(208) Image01Yvelocite=(+2)

;clavier gauche
If KeyDown(203) Image01Xvelocite=(-2)
;clavier droite
If KeyDown(205) Image01Xvelocite=(+2)

;/////////////////////////////////////////////////////////////////////////
;fin de la boucle
Wend
End


This looks like RectsOverlap.

What would be extremely useful would be a RectsAngleOverLap where the two rectangles can be at any 360 degree angle.

Floyd, my code is a bit different : it detects and blocks the 1st square if it collides with the 2nd square on his up, down, left or right. It is perfect for the ones who make, for example, a plateforme game and want there sprite/player stop when hiting a tile in different direction.

Enay, what about "Escape from reality" ? Is it finish ?

Well, RectsOverlap could be used for this, that would probably be faster and require less code. No offense!

Nope, it is not finish.

Nope, it is not finish.

No, its English.

??? RectsOverlaps could work too, but my code does not just check a simple collision.

Actually, JoeGr, the language is spelled Finnish :-)

If your function was called CollideRects or CollideImages instead of ImagesOverlap people would understand it's use better I think.