GLBasic forum

Codesnippets => 2D-snippets => Topic started by: backslider on 2012-May-31

Title: Box2D - Chain Type
Post by: backslider on 2012-May-31
Hi guys,

I created a simple Type for creating chains like a bridge or a lamp with the Box2D - Wrapper.

Code (glbasic) Select

// --------------------------------- //
// Project: box2dSoftbody
// Start: Thursday, May 31, 2012
// IDE Version: 10.202


// SETCURRENTDIR("Media") // go to media files

b_InitWorld(0,0,640,480,0,.96,TRUE)
b_CreateGroundbox(320,440,640,80)


//------------------------CHAIN------------------------
//Init the chain
LOCAL myChain AS TChain

myChain.AddChainLink(100,300, 0,1,0,10) //create the "sticked" parent

//create 20 chain link childs
FOR i% = 0 TO 49
myChain.AddChainLink(110 + i*10,300, 1,10,0,10)
NEXT

myChain.AddChainLink(610,300, 0,1,0,10)

myChain.Generate() //generate the "physics" chain"
//------------------------CHAIN------------------------

b_CreateBox(50,10,0,300,200,25,25)

//main loop
WHILE TRUE
b_Update(.1, 10)

b_WorldDebug()
SHOWSCREEN
WEND



//CHAIN TYPES
TYPE TChain
chainLinks[] AS TChainLink

//Add a link to the chain
FUNCTION AddChainLink: x%, y%, mass#, friction#, restitution#, size#
LOCAL link AS TChainLink
link.Init(x, y, mass, friction, restitution, size)
DIMPUSH self.chainLinks[], link
ENDFUNCTION

//Generate the chain after creating the links
FUNCTION Generate:
FOR i% = 0 TO LEN(self.chainLinks[]) - 1
LOCAL link AS TChainLink
link = self.chainLinks[i]
link.body = b_CreateCircle(link.mass, link.friction, link.restitution, link.x, link.y, link.size)
self.chainLinks[i] = link

IF i > 0
LOCAL linkBefore AS TChainLink
linkBefore = self.chainLinks[i-1]
DEBUG linkBefore.body + ", " + link.body + "\n"
link.joint = b_JointDistance(link.body, linkBefore.body, link.GetX(), link.GetY(), linkBefore.GetX(), linkBefore.GetY())
ENDIF
NEXT
ENDFUNCTION
ENDTYPE

TYPE TChainLink
x%
y%
mass#
friction#
restitution#
size
body%
joint%

FUNCTION Init: x%, y%, mass#, friction#, restitution#, size#
self.x = x
self.y = y
self.mass = mass
self.friction = friction
self.restitution = restitution
self.size = size / 2
ENDFUNCTION

FUNCTION GetX%:
RETURN b_BodyGetPosX(self.body)
ENDFUNCTION

FUNCTION GetY%:
RETURN b_BodyGetPosY(self.body)
ENDFUNCTION
ENDTYPE


Hope you like it!  :good:
Title: Re: Box2D - Chain Type
Post by: bigsofty on 2013-Apr-23
Better late than never.  :D Thanks Backslider, very handy!  :good:
Title: Re: Box2D - Chain Type
Post by: mentalthink on 2013-Apr-23
Thanks I think in 3D can be very handly!!! thaks for the work!!!  :good:
Title: Re: Box2D - Chain Type
Post by: backslider on 2013-Apr-25
You're welcome ;) I hope it helps you!
Would like to see it in a game. :)