Rotate a chain
by yurembo · in Torque 2D Beginner · 01/20/2016 (8:15 am) · 1 replies
I took up a sample cod from SandBox:
How can I rotate the chain from it's center.
I tried this code:
How can I rotate a chain?
function CreateChain(%posX, %posY)
{
// Set-up some initial dimensions.
%linkWidth = 3;
%linkHeight = %linkWidth * 3;
%halfLinkHeight = %linkHeight * 0.5;
%weightSize = 18;
%weightHalfSize = %weightSize * 0.5;
%pivotDistance = %linkHeight * $ChainLinks;
// Create a fixed pivot object.
%fixedObject = createPole();
// Set-up the last linked object as the fixed pivot.
%lastLinkObj = %fixedObject;
// Now add the rest of the links.
for ( %n = 1; %n <= $ChainLinks; %n++ )
{
// Create the link object.
if (%n == 1)
%obj = new Sprite(chain);
else
%obj = new Sprite();
%obj.setImage( "RopeStone:chain" );
%obj.setPosition( %posX + (%n*%linkHeight), %posY );
%obj.setSize( %linkWidth, %linkHeight );
%obj.setDefaultDensity( 30 );
%obj.setDefaultFriction( 0.3 );
%obj.createPolygonBoxCollisionShape( %linkWidth, %linkHeight );
%obj.SceneLayer = 31;
myScene.add( %obj );
// Create a revolute joint from the last link to this one.
if (%n == 1)
myScene.createRevoluteJoint( %lastLinkObj, %obj, 0, 0, 0, %halfLinkHeight, false );
else
myScene.createRevoluteJoint( %lastLinkObj, %obj, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
// Reference this link as the last link.
%lastLinkObj = %obj;
}
// Create the weight.
%weight = createStone();
// Create a revolute joint from the last link to the weight.
myScene.createRevoluteJoint( %lastLinkObj, %weight, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
}This is working fine: a stone weights in the chain.How can I rotate the chain from it's center.
I tried this code:
chain.moveTo("10 10", 100);andchain.rotateTo(20, 10);But this works uncorrect.
How can I rotate a chain?
About the author
Torque fan
Torque Owner Richard Ranft
Roostertail Games
You can of course swing a chain by affixing one end to a motor - might need to use some sort of arm to get enough distance from the center of rotation so that the chain will swing instead of just wiggle....
If you're looking to make a line-based walkable surface use the edge collision shape instead (unless you want it to flex and bounce like walking a rope bridge).