anglerVelocity Affects linearVelocity

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

try local bv = self.ball.linearVelocity

I’ve never needed to use getLinearVelocityFromWorldPoint, I never even knew it existed. Also try to have multi at a value around 1.05

@Goatboy76 Why are you getting the linearVelocity at point (1,1). What you need to do is get the linearVelocity of where the ball currently is. Try your code after changing the original line with the two lines I show. @Luatee That’s the suggestion I gave him when he posted something similar back on May 9th. See the link.

Link to similar post.

http://codea.io/talk/discussion/5057/setting-linear-velocity-of-physics-body#Item_4

Original code plus the corrections.


    local bv = self.ball:getLinearVelocityFromWorldPoint(vec2(1,1))  


    local wp=self.ball.position   -- get current position of ball
    local bv = self.ball:getLinearVelocityFromWorldPoint(wp)  -- get velocity at point wp

@Goatboy76 After further investigation, “getLinearVelocityFromWorldPoint” gets the velocity of a point on the body in question. My example gets the linear velocity of the center point of the body, so the center point is unaffected by any angular rotation. In your example, you’re getting the linear velocity of a point (1,1) on that body. So if the center of the ball is at (100,100) then you’re getting the velocity of a point at a distance of 99 pixels away from the center. So depending on the angular velocity and the position of the ball on the screen, your linear velocity can vary quite a bit. That will explain why the ball shoots off the screen at a high rate or changes direction. So from the info I have, “getLinearVelocityFromWorldPoint” and “angularVelocity” are working the way they’re supposed to.

@dave1707 Thanks for the help. I suspected somehting was funky with getLinearVelocity from world point. So is self.ball.linearVelocity the same as getLinearVelocityFromWorldPoint(pos.x,pos.y)?

@Goatboy76 Yes.