Hello. A while a go I started multiplying a physics body’s linearVelocity to give it a “boost” in its current vector. But this started doing some strange things like sending the ball speeding off its current course in directions that were far from its previous. After a bit of investigation I drew a parallel between these strange “boosts” and its angularVelocity. By setting the angularVelocity to zero after I “boost” it, the problems stop.
Does anyone know why this happens? The program below demonstrates this. Touching the screen will boost the ball. The parameter turns on and off wether it sets the angular velo to 0 after it boosts it. On is when it does zero the velo and off is when it doesnt.
-- Boost
supportedOrientations(LANDSCAPE_ANY)
function setup()
parameter.boolean("angleBool", 0)
print(angleBool)
oldAngleBool = angleBool
ball = Ball()
wallLeft = physics.body(EDGE, vec2(0,0), vec2(0, HEIGHT))
wallBot = physics.body(EDGE, vec2(WIDTH, 0), vec2(0,0))
wallTop = physics.body(EDGE, vec2(0,HEIGHT), vec2(WIDTH, HEIGHT))
wallRight = physics.body(EDGE, vec2(WIDTH, 0), vec2(WIDTH, HEIGHT))
end
function draw()
background(40, 40, 50)
ball:draw()
if oldAngleBool ~= angleBool then
ball:SetAngleBool(angleBool)
oldAngleBool = angleBool
end
end
function GetAngleBool()
return angleBool
end
function touched(touch)
ball:Boost()
end
Ball = class()
function Ball:init()
self.ball = physics.body(CIRCLE, 50)
self.ball.restitution = .6
self.ball.x = 50
self.ball.y = 50
self.ball.gravityScale = 0
self.ball.linearVelocity = vec2(10,5)
self.angleBool = GetAngleBool()
print(self.ball.angularVelocity)
end
function Ball:draw()
pushStyle()
fill(239, 12, 12, 255)
ellipse(self.ball.x, self.ball.y, 100)
end
function Ball:Boost()
if self.angleBool == true then
print("self.ball.angularVelocity = 0")
self.ball.angularVelocity = 0
end
local bv = self.ball:getLinearVelocityFromWorldPoint(vec2(1,1))
local multi = 1.3
local newVelo = bv * multi
self.ball.linearVelocity = newVelo
end
function Ball:SetAngleBool(bool)
self.angleBool = bool
end