Author Topic: Box2D - Chain Type  (Read 2699 times)

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Box2D - Chain Type
« 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:

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2645
    • View Profile
Re: Box2D - Chain Type
« Reply #1 on: 2013-Apr-23 »
Better late than never.  :D Thanks Backslider, very handy!  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3366
  • Integrated Brain
    • View Profile
Re: Box2D - Chain Type
« Reply #2 on: 2013-Apr-23 »
Thanks I think in 3D can be very handly!!! thaks for the work!!!  :good:

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: Box2D - Chain Type
« Reply #3 on: 2013-Apr-25 »
You're welcome ;) I hope it helps you!
Would like to see it in a game. :)