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) ))