The problem is exactly that.
The game is this: a shuttle moves and must avoid the obstacles. Obstacles must bounce on the walls. I thought of three different modes, one with a background: square, spherical and triangular. I want the obstacles to bounce back to infinity and not that it slides to infinity.
You asked me for a simple example and that’s what I did. I think the problem is this and that further code does not help.
However if you are asking for it I have no problems. I repeat I’m not reinventing the wheel so if you believe it can be useful here is:
local physics = require("physics") physics.setDrawMode( "hybrid" ) --debug hybrid normal physics.start() physics.setGravity( 0, 0 ) --"math2do" taken from the examples of @roaminggamer local math2do = {} local mRad = math.rad local mCos = math.cos local mSin = math.sin local mSqrt = math.sqrt function math2do.scale( ... ) -- ( objA, scale [, altRet] ) or ( x1, y1, scale, [, altRet] ) if( type(arg[1]) == "number" ) then local x,y = arg[1] \* arg[3], arg[2] \* arg[3] if(arg[4]) then return { x=x, y=y } else return x,y end else local x,y = arg[1].x \* arg[2], arg[1].y \* arg[2] if(arg[3]) then return x,y else return { x=x, y=y } end end end function math2do.length( ... ) -- ( objA ) or ( x1, y1 ) local len if( type(arg[1]) == "number" ) then len = mSqrt(arg[1] \* arg[1] + arg[2] \* arg[2]) else len = mSqrt(arg[1].x \* arg[1].x + arg[1].y \* arg[1].y) end return len end function math2do.normalize( ... ) -- ( objA [, altRet] ) or ( x1, y1 [, altRet] ) if( type(arg[1]) == "number" ) then local len = math2do.length( arg[1], arg[2], false ) local x,y = arg[1]/len,arg[2]/len if(arg[3]) then return { x=x, y=y } else return x,y end else local len = math2do.length( arg[1], arg[2], true ) local x,y = arg[1].x/len,arg[1].y/len if(arg[2]) then return x,y else return { x=x, y=y } end end end function math2do.angle2Vector( angle, tableRet ) local screenAngle = mRad(-(angle+90)) local x = mCos(screenAngle) local y = mSin(screenAngle) if(tableRet == true) then return { x=-x, y=y } else return -x,y end end --=========-- --border-- --=========-- local points = { 185.06664671286 ,51.577059737104, 209.73797743297 ,56.283367774274, 233.62491053694 ,64.04470282235, 256.35073482034 ,74.738663991227, 277.55705045849 ,88.196601125011, 296.90942118574 ,104.20627451572, 314.10264855516 ,122.51520205026, 328.8655851004 ,142.8346410042, 340.9654104932 ,164.84414168699, 350.21130325903 ,188.19660112501, 356.45745014574 ,212.52373708286, 359.60534568565 ,237.44189609414, 359.60534568565 ,262.55810390586, 356.45745014574 ,287.47626291714, 350.21130325903 ,311.80339887499, 340.9654104932 ,335.15585831301, 328.8655851004 ,357.1653589958, 314.10264855516 ,377.48479794974, 296.90942118574 ,395.79372548428, 277.55705045849 ,411.80339887499, 256.35073482034 ,425.26133600877, 233.62491053694 ,435.95529717765, 209.73797743297 ,443.71663222573, 185.06664671286 ,448.4229402629, 160, 450, 134.93335328714 ,448.4229402629, 110.26202256703 ,443.71663222573, 86.375089463064 ,435.95529717765, 63.649265179657 ,425.26133600877, 42.442949541505 ,411.80339887499, 23.090578814262 ,395.79372548428, 5.8973514448423 ,377.48479794974, -8.8655851004031 ,357.1653589958, -20.965410493204 ,335.15585831301, -30.211303259031 ,311.80339887499, -36.457450145738 ,287.47626291714, -39.605345685654 ,262.55810390586, -39.605345685654 ,237.44189609414, -36.457450145738 ,212.52373708285, -30.211303259031 ,188.19660112501, -20.965410493204 ,164.84414168699, -8.865585100403 ,142.8346410042, 5.8973514448422 ,122.51520205026, 23.090578814262 ,104.20627451572, 42.442949541505 ,88.196601125011, 63.649265179657 ,74.738663991227, 86.375089463064 ,64.04470282235, 110.26202256703 ,56.283367774274, 134.93335328714 ,51.577059737104, 160, 50, } local obstacle = display.newRect( 0, 0, 10, 10) physics.addBody( obstacle, "static", { chain = points, connectFirstAndLastChainVertex = true } ) --[[local Effetti = require("libs.Effetti") Effetti.setPathCircle( obstacle, { radius = 200, startA = 0, endA = 360, numPath = 50, })]]-- --=======================-- --ball(obstacle for ship)-- --=======================-- local ballSpeed = 200 local ball = display.newCircle( 250, 350, 15 ) physics.addBody( ball, "dynamic", {density=0.1, radius=15, bounce=0.7, friction=1 } ) ball.isBullet = true local enterFrame = function( self ) local vx,vy = self:getLinearVelocity() vx,vy = math2do.normalize( vx, vy ) --normalize ==\> normalizeVec vx,vy = math2do.scale( vx, vy, ballSpeed ) --scale ==\> scaleVec self:setLinearVelocity( vx, vy ) end local angleStart = 180 local vec = math2do.angle2Vector( angleStart, true ) vec = math2do.scale( vec, ballSpeed ) ball:setLinearVelocity( vec.x, vec.y ) ball.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", ball )
(start the code and wait about 10 seconds)