I have four circles as physics objects joined together by a sensor anchor object (in the centre) and to each other with weld joints (in a ring.)
For some reason, they spin. I don’t apply force of any kind and if I try to stop it with damping they continue.
If I add touch joints to move them around eventually the effect gets so bad they spin with almost infinite force and can’t be stopped with any amount of damping.
Can anyone tell me what I’m doing wrong here, please?
The code below is not the literal simplest way of demonstrating the problem, but it is a very simplified version of my code and demonstrates the problem.
[lua]-- spinning balls
display.setStatusBar(display.HiddenStatusBar)
require(“physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
–require(“toucheventlib”)
– rotates a point around the (0,0) point by degrees
– returns new point object
function rotateTo( point, degrees )
local x, y = point.x, point.y
local theta = math.rad(degrees)
local pt = {
x = x * math.cos(theta) - y * math.sin(theta),
y = x * math.sin(theta) + y * math.cos(theta)
}
return pt
end
function midPoint( pts )
local x, y, c = 0, 0, #pts
if (pts.numChildren and pts.numChildren > 0) then c = pts.numChildren end
for i=1, c do
x = x + pts[i].x
y = y + pts[i].y
end
return { x=x/c, y=y/c }
end
function newBall( params )
local group = display.newGroup()
group.x, group.y = params.x, params.y
local circle = display.newCircle( group, 0, 0, params.radius )
local c = params.colour
circle:setFillColor( c.r,c.g,c.b )
physics.addBody( group, {bounce=.8, friction=0, density=.1, radius=params.radius} )
group.linearDamping = 10
group.angularDamping = 1000
return group
end
function newGroup( balls )
– get midpoint of the balls to be anchored together
local pt = midPoint( balls )
– create anchor at that location
local anchor = display.newCircle( pt.x, pt.y, 38 )
anchor.alpha = .3
physics.addBody( anchor, {radius=38, isSensor=true} )
– store the list of balls
anchor.balls = balls
– join the balls to the anchor
for i=1, #balls do
local ball = balls[i]
physics.newJoint( “weld”, anchor, ball, anchor.x, anchor.y, ball.x, ball.y )
ball.anchor = anchor
end
–[[emergency fix - join balls to each other in a circle - does not work!]]–
local a, b = nil, nil
for i=1, #balls do
a = balls[i]
b = balls[i+1]
if (i == #balls) then b = balls[1] end
a.nextjoint = physics.newJoint( “weld”, a, b, a.x, a.y, b.x, b.y )
end
– return the anchor
return anchor
end
– create the balls
local balls = {}
for i=1, 4 do
local pt = rotateTo( {x=20,y=0}, 360/4*i )
balls[#balls+1] = newBall{ colour={r=0,g=0,b=255}, radius=38, x=display.contentCenterX+pt.x, y=display.contentCenterY+pt.y }
end
local anchor = newGroup( balls )[/lua] [import]uid: 8271 topic_id: 33409 reply_id: 333409[/import]