Simulating a robe/chain in TGB.
by Julian Liebl · in Torque Game Builder · 05/28/2011 (10:02 am) · 5 replies
Hello,
for our little game a robe or even chain would be awesome. Now TGB has Box2D as physics core right?
It is possible to archieve this in Box2D (non believers look here: http://www.emanueleferonato.com/2009/10/05/basic-box2d-rope/).
Since month I am trying to figure out a way to archieve this. Here is my code so far:
So what I do is simple: Startpoint is the object the behavior is active. From there it generates segments of the chain. First segmenent mounted to the owner. Second mounted to the first. Third mounted to the second ans so on.
The results till now are almost all useless. So we were lucky and figured out while trying to simulate a chain to simulate a hanging bridge ;).
Thank you for any help!
for our little game a robe or even chain would be awesome. Now TGB has Box2D as physics core right?
It is possible to archieve this in Box2D (non believers look here: http://www.emanueleferonato.com/2009/10/05/basic-box2d-rope/).
Since month I am trying to figure out a way to archieve this. Here is my code so far:
if (!isObject(createChainBehavior))
{
%template = new BehaviorTemplate(createChainBehavior);
%template.friendlyName = "Create a Chain";
%template.behaviorType = "Items";
%template.description = "Creates a chain.";
%template.addBehaviorField(imageMap, "Picture to take", string, '');
%template.addBehaviorField(imageMapX, "Image Size X", float, 10.0);
%template.addBehaviorField(imageMapY, "Image Size Y", float, 10.0);
%template.addBehaviorField(numberOfChainSeg, "Amount of chain segments to be created", float, 10.0);
}
function createChainBehavior::onLevelLoaded(%this)
{
%this.currentChainSegment = 0;
%this.mountForce = 0;
%this.mountOffsetY = 2.5;
%this.setDamping = 1;
for(;%this.currentChainSegment < %this.numberOfChainSeg; %this.currentChainSegment++){
%this.generateChain(false);
}
%this.generateChain(true);
}
function createChainBehavior::generateChain(%this,%last)
{
%pointX = %this.owner.getPositionX();
%pointY = %this.owner.getPositionY()+ (%this.currentChainSegment * imageMapY + 2);
%point = %pointX SPC %pointY;
%this.chains[%this.currentChainSegment] = new t2dStaticSprite()
{
scenegraph = %this.owner.scenegraph;
class = createChain;
//distance= %this.distance;
player = %this.owner;
position = %point;
imageMap = %this.imageMap;
imageMapX = %this.imageMapX;
imageMapY = %this.imageMapY;
};
%this.chains[%this.currentChainSegment].setLayer(%this.owner.getLayer());
%this.chains[%this.currentChainSegment].setSize(%this.imageMapX,%this.imageMapY);
//%this.chains[%this.currentChainSegment].setMaxLinearVelocity(1);
//%this.chains[%this.currentChainSegment].setAutoRotation(200.000);
%this.chains[%this.currentChainSegment].setMaxAngularVelocity(1.5);
if(%this.currentChainSegment == 0){
%this.chains[%this.currentChainSegment].mount(%this.owner,0,%this.mountOffsetY,%this.mountForce,false,true,false,false);
}else{
%this.chains[%this.currentChainSegment].mount(%this.chains[%this.currentChainSegment-1], 0, %this.mountOffsetY,%this.mountForce,false,true,false,false);
%this.chains[%this.currentChainSegment].setImmovable(false);
}
//%this.chains[%this.currentChainSegment].setCollisionLayers(0);
%this.chains[%this.currentChainSegment].setCollisionActive(true,true);
%this.chains[%this.currentChainSegment].setCollisionPhysics(true, true);
%this.chains[%this.currentChainSegment].setCollisionResponse(rigid);
%this.chains[%this.currentChainSegment].setDamping(0.300);
%this.chains[%this.currentChainSegment].setAutoMassInertia(true);
%this.chains[%this.currentChainSegment].setForceScale(1.000);
%this.chains[%this.currentChainSegment].setConstantForce(0.000, 5.000);
%this.chains[%this.currentChainSegment].setGraviticConstantForce(true);
%this.chains[%this.currentChainSegment].setDensity(0.010);
%this.chains[%this.currentChainSegment].setFriction(0.000);
%this.chains[%this.currentChainSegment].setRestitution(0.010);
}So what I do is simple: Startpoint is the object the behavior is active. From there it generates segments of the chain. First segmenent mounted to the owner. Second mounted to the first. Third mounted to the second ans so on.
The results till now are almost all useless. So we were lucky and figured out while trying to simulate a chain to simulate a hanging bridge ;).
Thank you for any help!
About the author
#2
05/28/2011 (3:14 pm)
Yah. I believe when you mount objects in TGB as is currently, they lose the ability to rotate around the joint. That is exactly what Box2D (the physics engine of Angry Birds uses) . I believe full, native implementation is on a long list of things to do for the staff. You can implement it yourself tho. Follow the link.
#3
05/28/2011 (3:41 pm)
Thank you very much. I thought all the time it is already implemented. Right now following the tutorial. Exited! Will post my success (or not) here.
#4
05/28/2011 (9:47 pm)
Cool. Good luck. I would be interested in hearing how easy or difficult and how reliable you find the implementation of Box2D.
#5
Biggest problem of all: Collisions in the Box2D World aren't 'compatible' with collisions in the TGB World. So for example if you got alot of boxes colliding in the Box2D World you can't collide with you TGB World Hero in the same Scene. Only way so far is to migrate your current level and of cause all enemys and heros itself into the Box2D world (hell of a work).
Also a problem: you are able to use all functions of Box2D with the wrapper for TGB. But they are of cause not the same as the TGB's. So there is a lot of work to do in reading those documentations to get familiar with Box2D in order to move objects and do some scripting stuff.
What is nice:
- 500 boxes and up still got 800 frames per second!
- physics are incredible!!
- really good documentation (sorry TGB ;) ).
- a lot of nice tutorials
- the combinations of TGB's Level editor and Box2D is not a bad decision
All in all we are still planning to jump of the train soon and joining shiva's steps or even might use sunburn engine instead.
Still hoping that the next TGB version might rock us all.
Cheers,
Julian
05/31/2011 (4:08 am)
Okay it was a bit of a work but finally got it working yesterday. Problem here is if you are already working on a TGB Project and now want to migrate those two, it is not easy.Biggest problem of all: Collisions in the Box2D World aren't 'compatible' with collisions in the TGB World. So for example if you got alot of boxes colliding in the Box2D World you can't collide with you TGB World Hero in the same Scene. Only way so far is to migrate your current level and of cause all enemys and heros itself into the Box2D world (hell of a work).
Also a problem: you are able to use all functions of Box2D with the wrapper for TGB. But they are of cause not the same as the TGB's. So there is a lot of work to do in reading those documentations to get familiar with Box2D in order to move objects and do some scripting stuff.
What is nice:
- 500 boxes and up still got 800 frames per second!
- physics are incredible!!
- really good documentation (sorry TGB ;) ).
- a lot of nice tutorials
- the combinations of TGB's Level editor and Box2D is not a bad decision
All in all we are still planning to jump of the train soon and joining shiva's steps or even might use sunburn engine instead.
Still hoping that the next TGB version might rock us all.
Cheers,
Julian
Torque Owner Alain Labrie
Ware-Wolf Games