und mal wieder was neues:
eine Standard-Kollision, die zurückgibt, an welcher seite die kollision stattfindet.
Code: Alles auswählen
EnableExplicit
; ******************************************************************************
; **
; ** SectorCollision( x1.l, y1.l, a1.l, b1.l, x2.l, y2.l, a2.l, b2.l )
; **
; ** by Kaeru Gaman ; Jan.2009 ; PB 4.3
; **
; ******************************************************************************
; **
; ** Checks if Boxes 1 and 2 collide
; **
; ** Returns 0 if no Collision
; ** Returns Sector relative to Box 2 if Collision
; ** Order of Sectors:
; **
; ** \ 1 /
; ** 2 # 4
; ** / 3 \
; **
Procedure.l SectorCollision( x1.l, y1.l, a1.l, b1.l, x2.l, y2.l, a2.l, b2.l )
Protected dx.l, dy.l
Protected sx.l, sy.l
Protected Coll.l = 0
dx = (x1 + a1/2) - (x2 + a2/2)
If dx < 0
dx = -dx
sx = -1
ElseIf dx > 0
sx = 1
EndIf
dy = (y1 + b1/2) - (y2 + b2/2)
If dy < 0
dy = - dy
sy = -1
ElseIf dy > 0
sy = 1
EndIf
If dx < (a1+a2)/2 And dy < (b1+b2)/2
If dx < dy
Coll = 2 + sy
Else
Coll = 3 + sx
EndIf
EndIf
ProcedureReturn Coll
EndProcedure
; ***
; *** Demo for SectorCollision
; ***
; *** Change if your display needs other values
#Screen_Width = 1024
#Screen_Height = 768
#Screen_Depth = 32
#SW1 = 64
#SH1 = 32
#SW2 = 128
#SH2 = 64
InitSprite()
InitKeyboard()
InitMouse()
OpenScreen( #Screen_Width, #Screen_Height, #Screen_Depth, "test")
CreateSprite( 0, #SW1, #SH1 )
StartDrawing( SpriteOutput( 0 ) )
DrawingMode(#PB_2DDrawing_Transparent)
Box( 0,0, #SW1, #SH1, $0080FF )
DrawText( 8,8,"A", $FFFFFF )
StopDrawing()
CreateSprite( 1, #SW2, #SH2 )
StartDrawing( SpriteOutput( 1 ) )
DrawingMode(#PB_2DDrawing_Transparent)
Box( 0,0, #SW2, #SH2, $004040 )
DrawText( 8,8,"B", $FFFFFF )
StopDrawing()
Define MX.l
Define MY.l
Define SX.l = #Screen_Width / 2 - #SW1 / 2
Define SY.l = #Screen_Height / 2 - #SH1 / 2
Define Colli.l
Repeat
ExamineKeyboard()
ExamineMouse()
MX = MouseX() : MY = MouseY()
ClearScreen( $302010 )
DisplaySprite( 1, SX, SY )
DisplaySprite( 0, MX, MY )
Colli = SectorCollision( MX, MY, #SW1, #SH1, SX, SY, #SW2, #SH2 )
StartDrawing( ScreenOutput() )
DrawingMode(#PB_2DDrawing_Transparent)
DrawText( 8,8, "Collision Sector: "+Str(Colli), $20C0FF )
StopDrawing()
FlipBuffers( #PB_Screen_SmartSynchronization )
Until KeyboardPushed( #PB_Key_Escape )