Setting Linear Velocity of physics.body

Hello. I’m trying to apply a speed boost to a ball. I use getLinearVelocityFromWorldPoint and .linearVelocity() to get the past speed, and then set the ball’s speed based on the boost. It calculates the boost to propel ball on its vector. What I need help with is, sometimes it works as it was designed to, giving the ball a small tap, while other times it sends the ball blazing off the screen at the speed of light.

I tried to upload this to CC but I keep getting Draw Corrupted, please restart the program. I uploaded an older version with this bug before I got this bug, its called Ball Project but BE SURE TO TAKE THE OLDEST VERSION! The newer ones have the bugged code commented out.

The code prints out a report on the previous velo, the difference the boost applies, the quadrant the velo is in, and the after velo every time the function is run

Here is just the speed boost code, the code on the CC wont adjust the boost to match the velocity.

Can someone please look over this on why this is happening?

function Ball:SpeedBoost()
    print("--------------------------------")
    
    local velo = self.ball:getLinearVelocityFromWorldPoint( vec2(1,1) )
    
    print("Before velo")
    
    print(self.ball:getLinearVelocityFromWorldPoint( vec2(1,1) ))
    
    local boost = .5
    
    if velo.x > 0 and velo.y > 0 then 
        -- Adjusts boost to match velocity vector
        if velo.x > velo.y then
            local boostMulti = velo.y / velo.x
            
             adjBoost = vec2(boost, boost * boostMulti)
        else
            local boostMulti = velo.x / velo.y  
                       
             adjBoost = vec2(boostMulti * boost, boost)
        end
            
    local newVelo = vec2(velo.x + adjBoost.x, velo.y + adjBoost.y)
    print(math.abs(newVelo.x) - math.abs(velo.x))
    self.ball.linearVelocity = vec2(newVelo.x, newVelo.y)
    print("Up right")
        
    elseif velo.x < 0 and velo.y < 0 then
         -- Adjusts boost to match velocity vector
        if velo.x > velo.y then
            local boostMulti = velo.y / velo.x
            
             adjBoost = vec2(boost, boost * boostMulti)
        else
            local boostMulti = velo.x / velo.y  
                       
             adjBoost = vec2(boostMulti * boost, boost)
        end
        
        local newVelo = vec2(velo.x - adjBoost.x, velo.y - adjBoost.y)
            print(math.abs(newVelo.x) - math.abs(velo.x))
        self.ball.linearVelocity = vec2(newVelo.x, newVelo.y)
        print("Down left")

    elseif velo.x > 0 and velo.y < 0 then
         -- Adjusts boost to match velocity vector
        if velo.x > velo.y then
            local boostMulti = velo.y / velo.x
            
             adjBoost = vec2(boost, boost * boostMulti)
        else
            local boostMulti = velo.x / velo.y  
                       
            adjBoost = vec2(boostMulti * boost, boost)
        end
        
        local newVelo = vec2(velo.x + adjBoost.x, velo.y - adjBoost.y)
        print(math.abs(newVelo.x) - math.abs(velo.x))        
        self.ball.linearVelocity = vec2(newVelo.x, newVelo.y)
        print("Down right")
        
    elseif velo.x < 0 and velo.y > 0 then
         -- Adjusts boost to match velocity vector
        if velo.x > velo.y then
            local boostMulti = velo.y / velo.x
            
             adjBoost = vec2(boost, boost * boostMulti)
        else
            local boostMulti = velo.x / velo.y  
                       
             adjBoost = vec2(boostMulti * boost, boost)
        end 
        
        local newVelo = vec2(velo.x - adjBoost.x, velo.y + adjBoost.y)
        print(math.abs(newVelo.x) - math.abs(velo.x))        
        self.ball.linearVelocity = vec2(newVelo.x, newVelo.y)
        print("Up Left")
        
    end  
    print("After velo")
    print(self.ball:getLinearVelocityFromWorldPoint( vec2(1,1) ))

@Goatboy76 I don’t use CC, so I didn’t load all of your code to try and run it to see what’s wrong. But here’s a simple example of what I do to change the linearVelocity of a physics body. Tapping the screen fast causes the ball to eventually rise. Tapping left of the ball causes it to go left, tapping right causes it to go right. I don’t know if this will help you any. If not, I’m sure others will help.


displayMode(FULLSCREEN)

function setup()
    ball=physics.body(CIRCLE,50)
    ball.x=WIDTH/2
    ball.y=HEIGHT
    ball.gravityScale=.1
end

function draw()
    background(40,40,50)
    fill(255)
    ellipse(ball.x,ball.y,100)  
end

function touched(t)
    if t.state==BEGAN then
        bv=ball.linearVelocity
        if t.x<ball.x then
            vx=-5 
        else
            vx=5
        end
        ball.linearVelocity=vec2(bv.x+vx,bv.y+10)
    end
end

@Goatboy76 If I understand what you’re trying to do, that is to increase the speed in the current direction, than you should be able to do something similar to the code below.


-- current ball velocity in some x,y direction
bv=ball.linearVelocity

-- increase the speed in the current x,y direction
ball.linearVelocity=vec2(bv.x*increase,bv.y*increase)

Sorry I took so long

@dave1707 Thanks for your help, I ended up using the simpler exponential way. I originally thought that it would be bad because things could spiral out of control really quickly but I just putt a speed cap at which it the boost would hurt more then help. Thanks again.