................
Publié : jeu. 09/juin/2005 22:57
.............
Code : Tout sélectionner
Enumeration
#SuperCollision_Haut = %0001
#SuperCollision_Droite = %0010
#SuperCollision_Bas = %0100
#SuperCollision_Gauche = %1000
EndEnumeration
en quoi c'est plus clair les constantes ?au lieu d'utiliser une chaîne de caractères...
pas seulement ! pour toute utilisation de sprite !sympa pour des briques ^^
Code : Tout sélectionner
collision = super_collision(...)
if collision = (#SuperCollision_Haut|#SuperCollision_Droite)
debug "collision en haut à droite"
endif
Code : Tout sélectionner
If y_sprite2<y_sprite1 And x_sprite2>=x_sprite1 And x_sprite2+largeur_spr2<=x_sprite1+largeur_spr1
bord.l=#haut
EndIf
Code : Tout sélectionner
Procedure.l MakeLong(Low.w, High.w)
ProcedureReturn (Low & $FFFF) | ((High << 16) & $FFFF0000)
EndProcedure
Procedure.w LowWord(Long.l)
ProcedureReturn Long & $FFFF
EndProcedure
Procedure.w HighWord(Long.l)
ProcedureReturn (Long >> 16) & $FFFF
EndProcedure
Procedure SpriteCollisionLocation(Sprite1.l, x1.l, y1.l, Sprite2.l, x2.l, y2.l)
Protected w1.l, h1.l, w2.l, h2.l
Protected xl.l, xr.l, yu.l, yd.l
Protected ax.l, ay.l, collision.l
Protected i.l, j.l, cx.l, cy.l, nb.l
Protected x.l, y.l, u.l, v.l, yy.l, vv.l
w1 = SpriteWidth (Sprite1)
h1 = SpriteHeight(Sprite1)
w2 = SpriteWidth (Sprite2)
h2 = SpriteHeight(Sprite2)
collision = #True
If x1 < x2
If (x1 + w1) > x2
xl = x2
u = 0
x = xl - x1
Else
collision = #False
EndIf
Else
If (x2 + w2) > x1
xl = x1
x = 0
u = xl - x2
Else
collision = #False
EndIf
EndIf
If y1 < y2
If (y1 + h1) > y2
yu = y2
v = 0
y = yu - y1
Else
collision = #False
EndIf
Else
If (y2 + h2) > y1
yu = y1
y = 0
v = yu - y2
Else
collision = #False
EndIf
EndIf
If collision = #True
If (x1 + w1) < (x2 + w2)
xr = x1 + w1
Else
xr = x2 + w2
EndIf
If (y1 + h1) < (y2 + h2)
yd = y1 + h1
Else
yd = y2 + h2
EndIf
EndIf
If collision = #True
Dim pt1.l(w1, h1)
If StartDrawing( SpriteOutput(Sprite1) )
For i = 0 To w1 - 1
For j = 0 To h1 - 1
pt1(i, j) = Point(i, j)
Next j
Next i
StopDrawing()
Else
collision = #False
EndIf
EndIf
If collision = #True
Dim pt2.l(w2, h2)
If StartDrawing( SpriteOutput(Sprite2) )
For i = 0 To w2 - 1
For j = 0 To h2 - 1
pt2(i, j) = Point(i, j)
Next j
Next i
StopDrawing()
Else
collision = #False
EndIf
EndIf
If collision = #True
nb = 0
yy = y
vv = v
For i = xl To xr
y = yy
v = vv
For j = yu To yd
If pt1(x, y) And pt2(u, v)
cx + i
cy + j
nb + 1
EndIf
y + 1
v + 1
Next j
x + 1
u + 1
Next i
If nb > 0
cx / nb
cy / nb
collision = MakeLong(cx, cy)
Else
collision = #False
EndIf
EndIf
ProcedureReturn collision
EndProcedure
InitSprite()
InitMouse()
InitKeyboard()
x = 500
y = 500
If OpenScreen(1024, 768, 16, "")
TransparentSpriteColor(#PB_Default, 0, 0, 0)
CreateSprite(0, 320, 240)
CreateSprite(1, 60, 60)
CreateSprite(2, 3, 3)
If StartDrawing( SpriteOutput(0) )
Ellipse(160, 120, 160, 120, $0000FF)
Ellipse(160, 120, 80, 60, $000000)
StopDrawing()
EndIf
If StartDrawing( SpriteOutput(1) )
Circle(0, 0, 60, $00FF00)
StopDrawing()
EndIf
If StartDrawing( SpriteOutput(2) )
Box(0, 0, 3, 3, $FF0000)
StopDrawing()
EndIf
Repeat
ExamineMouse()
ExamineKeyboard()
ClearScreen(0, 0, 0)
x + MouseDeltaX()
y + MouseDeltaY()
DisplayTransparentSprite(0, 80, 80)
DisplayTransparentSprite(1, x, y)
collision = SpriteCollisionLocation(0, 80, 80, 1, x, y)
If collision
cx = LowWord (collision)
cy = HighWord(collision)
DisplaySprite(2, cx-1, cy-1)
EndIf
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
CloseScreen()
EndIf
Code : Tout sélectionner
Procedure SpriteCollisionLocation(Sprite1.l, x1.l, y1.l, Sprite2.l, x2.l, y2.l, *location.POINTS)
Protected w1.l, h1.l, w2.l, h2.l
Protected xl.l, xr.l, yt.l, yb.l
Protected x.l, y.l, u.l, v.l, yy.l, vv.l
Protected i.l, j.l, cx.l, cy.l, nb.l, collision.l
w1 = SpriteWidth (Sprite1)
h1 = SpriteHeight(Sprite1)
w2 = SpriteWidth (Sprite2)
h2 = SpriteHeight(Sprite2)
collision = #True
If x1 < x2
If (x1 + w1) > x2
xl = x2
u = 0
x = xl - x1
Else
collision = #False
EndIf
Else
If (x2 + w2) > x1
xl = x1
x = 0
u = xl - x2
Else
collision = #False
EndIf
EndIf
If y1 < y2
If (y1 + h1) > y2
yt = y2
v = 0
y = yt - y1
Else
collision = #False
EndIf
Else
If (y2 + h2) > y1
yt = y1
y = 0
v = yt - y2
Else
collision = #False
EndIf
EndIf
If collision = #True
If (x1 + w1) < (x2 + w2)
xr = x1 + w1
Else
xr = x2 + w2
EndIf
If (y1 + h1) < (y2 + h2)
yb = y1 + h1
Else
yb = y2 + h2
EndIf
xr - 1
yb - 1
w1 - 1
h1 - 1
w2 - 1
h2 - 1
EndIf
If collision = #True
Dim pt1.l(w1, h1)
If StartDrawing( SpriteOutput(Sprite1) )
For i = xl To xr
For j = yt To yb
pt1(i-xl+x, j-yt+y) = Point(i-xl+x, j-yt+y)
Next j
Next i
StopDrawing()
Else
collision = #False
EndIf
EndIf
If collision = #True
Dim pt2.l(w2, h2)
If StartDrawing( SpriteOutput(Sprite2) )
For i = xl To xr
For j = yt To yb
pt2(i-xl+u, j-yt+v) = Point(i-xl+u, j-yt+v)
Next j
Next i
StopDrawing()
Else
collision = #False
EndIf
EndIf
If collision = #True
yy = y
vv = v
For i = xl To xr
y = yy
v = vv
For j = yt To yb
If pt1(x, y) And pt2(u, v)
cx + i
cy + j
nb + 1
EndIf
y + 1
v + 1
Next j
x + 1
u + 1
Next i
If nb > 0 And *location
*location\x = cx / nb
*location\y = cy / nb
EndIf
EndIf
ProcedureReturn nb
EndProcedure
InitSprite()
InitMouse()
InitKeyboard()
x = 500
y = 500
If OpenScreen(1024, 768, 16, "")
TransparentSpriteColor(#PB_Default, 0, 0, 0)
CreateSprite(0, 320, 240)
CreateSprite(1, 60, 60)
CreateSprite(2, 3, 3)
If StartDrawing( SpriteOutput(0) )
Ellipse(160, 120, 160, 120, $0000FF)
Ellipse(160, 120, 80, 60, $000000)
StopDrawing()
EndIf
If StartDrawing( SpriteOutput(1) )
Circle( 0, 30, 30, $00FF00)
Circle(60, 30, 30, $00FF00)
Circle(30, 30, 20, $000000)
StopDrawing()
EndIf
If StartDrawing( SpriteOutput(2) )
Box(0, 0, 3, 3, $FF0000)
StopDrawing()
EndIf
Repeat
ExamineMouse()
ExamineKeyboard()
ClearScreen(0, 0, 0)
x + MouseDeltaX()
y + MouseDeltaY()
DisplayTransparentSprite(0, 80, 80)
DisplayTransparentSprite(1, x, y)
If SpriteCollisionLocation(0, 80, 80, 1, x, y, @Location.POINTS)
DisplaySprite(2, Location\x-1, Location\y-1)
EndIf
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
CloseScreen()
EndIf