News:

Function Finder  Find all the functions within source code files

Main Menu

Attaching Shapes To Characters for Multiple Collision Zones

Started by kevin, January 14, 2013, 09:14:57 AM

Previous topic - Next topic

kevin

  
Attaching Shapes To Characters for Multiple Collision Zones

  In this quickly slapped together example, we're attaching a group of randomly created and  grouped shapes to our 'dude' character.  Using multiple shapes allows the programmer to not only detect impacts with the main character as a whole, but locate more specific impact upon a particular zone, such as arms, legs, body.. or whatever components within the image you're interested in.    


PlayBASIC Code: [Select]
; PROJECT : Attached-Shape-Collision-Zones
; AUTHOR : Kevin Picone
; CREATED : 1/15/2013
; EDITED : 1/15/2013
; ---------------------------------------------------------------------

; ---------------------------------------------------
; Define the character structure this program will use
; ---------------------------------------------------

Type tBody
; SCreen Coordinates of this dude
Xpos#
Ypos#

; rotation offset
HandleX#
HandleY#

; image index to use as it's representation on screen
IMage

; angle of this character
Angle#


; Turn Speed
TurnSpeed#

; Colour the attached collision shapes
ShapeColour

; number of the collision shapes attached to this dude
ShapeCount

; array with thye shape indexs in them
Shapes(16)
EndType


; ---------------------------------------------------
; The Global array of dudes the scene with use
; ---------------------------------------------------

Dim Dude(0) as tBody


; ---------------------------------------------------
; create a bunch of dudes for this scene. Each dude
; has randomly created with random number of
; ---------------------------------------------------
For lp=1 to 10
; create dude /character
size=rndrange(128,256)
INdex=NewDude(rnd(800),rnd(600),size,size)


for rects=0 to 3+rnd(5)

ShapeSize=rndrange(20,30)

offsetX=rndrange(0,Size-ShapeSize)
OffsetY=rndrange(0,Size-ShapeSize)

ThisShape=NewConvexShape(ShapeSize,3+rnd(6))
ShiftSHape ThisShape,OffsetX,OffsetY
AddDudeShape(Index,ThisShape)

next


OffsetX#=Size/-2.0
OffsetY#=Size/-2.0
ShiftDudeHandle(index,OffsetX#,OffsetY#)

next



; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; --[ MAIN LOOP ] -----------------------------
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------

; limit app speed to a max of 30 frames per second
Setfps 30


; create an FX image the size of the screen
Screen=NewIMage(800,600,2)



Do

rendertoimage screen

; clear the backdrop
cls $304050

; draw the all the dudes
Draw_ALL_DUdes()


; render the screen image to actual screen
rendertoscreen
drawimage screen,0,0,false


; flip back buffer to front
Sync
loop esckey()=true


End




; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; --[ Draw All The Dudes in the scene (linear) ] -----------------------------
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------

Function Draw_ALL_DUdes()

For lp =1 to GetArrayElements(Dude())
if Dude(lp)

x#=Dude(lp).Xpos
y#=Dude(lp).Ypos
HandleX#=Dude(lp).HandleX
HandleY#=Dude(lp).HandleY

Angle#=wrapangle(Dude(lp).angle,Dude(lp).TurnSpeed)

img=Dude(lp).IMage

Width#=GetImageWidth(Img)
Height#=GetImageHeight(Img)


DrawRotatedIMage Img,x#,y#,Angle#,1,1,HandleX#,HandleY#,false+8

Dude(lp).angle=Angle#

Login required to view complete source code