I'm trying to understand how they work. Here's an example of trying to make a bridge using line joints.
setVirtualResolution(640,480)
gap = 10
chunks = 5
x = getVirtualWidth() - (chunks+1)*gap - 10
width = x / chunks
dim planks[chunks]
for i = 1 to chunks
planks[i] = createSprite(0)
setSpriteSize(planks[i], width, 20)
x = 5+(gap*i)+(i-1)*width
setSpritePosition(planks[i], x, 240)
setSpritePhysicsOn(planks[i], 2)
if i > 1
j = createLineJoint(planks[i-1], planks[i], x, 240, 1, 1, 1)
setJointLimitOn(j, 0, gap*2)
endif
next i
anchor1 = createSprite(0)
setSpriteSize(anchor1, 5, 5)
setSpritePosition(anchor1, 5, 240)
setSpritePhysicsOn(anchor1, 3)
anchor2 = createSprite(0)
setSpriteSize(anchor2, 5, 5)
setSpritePosition(anchor2, getVirtualWidth()-5, 240)
setSpritePhysicsOn(anchor2, 3)
createLineJoint(anchor1, planks[1], 0, 240, 1, 1, 1)
createLineJoint(anchor2, planks[chunks], getVirtualWidth(), 240, 1, 1, 1)
setPhysicsDebugOn()
repeat
sync()
until getRawKeyPressed(27) = 1
end
I'm trying to use SetJointLimitOn to limit the amount of spring between joints, and even though the help file specifically states:
"Works on Line joints, Prismatic joints, and Revolute joints"
I get an error saying:
"Attempted to set a joint limit on a joint that doesn't support limits".
My other question is, how can I control where the joint connects to on the 2nd sprite? I would think the intuitive thing would be to specify the anchor point on both sprites, not just one.
edit
Just discovered the distance joint is more of what I want to use. Still, is there a bug with joint limits?
Here's another issue I'm seeing. Run this example and you'll notice how the far right joint stretches out very far. Kind of defeating the purpose of using a distance joint. And my "bridge" is constantly moving.
setVirtualResolution(640,480)
gap = 4
chunks = 12
x = getVirtualWidth() - (chunks+1)*gap - 10
width = x / chunks
dim planks[chunks]
for i = 1 to chunks
planks[i] = createSprite(0)
setSpriteSize(planks[i], width, 20)
x = 5+(gap*i)+(i-1)*width
setSpritePosition(planks[i], x, 240)
setSpritePhysicsOn(planks[i], 2)
if i > 1
`j = createLineJoint(planks[i-1], planks[i], x-(gap/2), 240, 1, 1, 0)
createDistanceJoint(planks[i-1], planks[i], x-gap, 240, x, 240, 0)
`setJointLimitOn(j, 0, gap*2)
endif
next i
anchor1 = createSprite(0)
setSpriteSize(anchor1, 5, 5)
setSpritePosition(anchor1, 0, 240)
setSpritePhysicsOn(anchor1, 3)
anchor2 = createSprite(0)
setSpriteSize(anchor2, 5, 5)
setSpritePosition(anchor2, getVirtualWidth()-5, 240)
setSpritePhysicsOn(anchor2, 3)
createDistanceJoint(anchor1, planks[1], 0, 240, gap, 240, 0)
createDistanceJoint(anchor2, planks[chunks], getVirtualWidth(), 240, getVirtualWidth()-gap, 240, 0)
setPhysicsDebugOn()
repeat
if getRawMouseLeftPressed()
n = createSprite(0)
setSpriteSize(n, 30, 30)
setSpriteColor(n, random(0,255),random(0,255),random(0,255),255)
setSpritePosition(n, getPointerX(), getPointerY())
setSpritePhysicsOn(n, 2)
setSpriteAngle(n, random(0,360))
endif
sync()
until getRawKeyPressed(27) = 1
end