Hey all,
I’m playing around with the physics engine, but I came upon an issue I cannot seem to resolve; I spawn a bunch of circles (15), add a dynamic body to it, and give them a random velocity : (simplified code)
globe = display.newCircle(
math.random(30, FIELD.WIDTH-30),
math.random(30, FIELD.HEIGHT-30),
5 )
physics.addBody( globe, "dynamic", { radius=5, bounce=0.99 } )
globe.setLinearVelocity(Xv, Yv)
I have my “playfield” bordered by rectangles that are “static” body;
bot = rect(0, FIELD.HEIGHT, FIELD.WIDTH, 15, green)
right = rect(FIELD.WIDTH-5, 0, 15, FIELD.HEIGHT, purple)
top = rect(0, 0, FIELD.WIDTH, 15, green)
left = rect(0, 0, 5, FIELD.HEIGHT, red)
physics.addBody( top, "static" )
physics.addBody( bot, "static" )
physics.addBody( left, "static" )
physics.addBody( right, "static" )
function rect(x, y, w, h, color)
local new_x = x+(w/2)
local new_y = y+(h/2)
local r = display.newRect(new_x, new_y, w, h)
r.fill = color
return r
end
After running the simulation for a few seconds, some globes get stuck to the wall; I’m not exactly sure what happens, but I observe that Xv or Yv drops to 0 in this state, my attempt to fix this was to run check regularly for this case and reset Xv or Yv to the initial state (which I store); while this sometimes fixes it, mostly it does not; (simplified)
if (vx == 0 or vy == 0) then
new_vx = vx
new_vy = vy
if (vx == 0) then
new_vx = globe[id].init_Xv
end
if (vy == 0) then
new_vy = globe[id].init_Yv
end
globe[id]:setLinearVelocity(new_vx, new_vy)
end
I also attempted to make the border not straight by adding, but again no luck;
r.path.x4 = -1
r.path.x3 = -2
r.path.x2 = -3
r.path.x1 = -4
I then tried to round the x and y position because I believe it might be related to floating calculations; so I round the numbers and reset Xv and Yv; (depending on what is 0) Sadly this does not resolve it either; The last thing I tried is to teleport them away from the wall while resetting :
globe[id].x = round(globe[id].x) + (math.random(20,30)/10)
globe[id].y = round(globe[id].y) + (math.random(20,30)/10)
globe[id]:setLinearVelocity(new_vx, new_vy)
While this helps somewhat, its clearly visible, and not a full solution; Does anyone have an idea how to resolve this ?
This is what I want to recreate : https://upload.wikimedia.org/wikipedia/commons/6/6d/Translational_motion.gif
Thanks for the help / idea’s !