I’m getting some really weird results when calling body:applyForce each frame.
I’m currently trying to work through Chapter 6 of Nature of Code, and I know it’s not practical, but I’m calling body:applyForce on every touch event, just to be as close to the Processing examples as possible.
At the moment my main.lua file is this:
require "physics" physics.start() physics.setGravity(0,0) require "vehicle" local v=vehicle.create() Runtime:addEventListener("touch",function(event) if event.phase=="moved" or event.phase=="began" then v:setTarget(event.x,event.y) end end)
and vehicle.lua looks like this:
local M={} vehicle=M local display=display local math=math require "physics" local physics=physics setfenv(1,M) local maxSpeed=10 local maxForce=10 function create() local img=display.newImage("img/car.png") physics.addBody(img) function img:steer() if not self.targetX then return end local dx,dy=self.targetX-self.x,self.targetY-self.y local d2=dx\*dx+dy\*dy local invMag=maxSpeed/d2^0.5 dx=dx\*invMag dy=dy\*invMag local vx,vy=self:getLinearVelocity() local sx,sy=dx-vx,dy-vy local force2=sx\*sx+sy\*sy if force2\>maxForce\*maxForce and force2\>0 then local r=maxForce/(force2^0.5) sx=sx\*r sy=sy\*r end -- self:setLinearVelocity(vx+sx,vy+sy) self:applyForce(sx,sy,self.x,self.y) end local target function img:setTarget(x,y) self.targetX=x self.targetY=y if target then target:removeSelf() end target=display.newCircle(x,y,20) target:setFillColor(255,0,0) self:steer() end function img:update() self:steer() end return img end return M
When I run it using applyForce calls in vehicle:steer the thing quickly gets juddery and flies of the screen (slowly) if I replace it with the simple call to setLinearVelocity the thing works fine. Am I doing something wrong, or is this a bug with the the applyForce call?