As my character limited title suggests I was working on a function to alter my balls' horizontal velocity based on how far it was from center. I decided to cut my paddle into 5ths, and apply a certain ratio to the horizontal velocity based on what section it hit. The outer sections would have the most increase while the innermost would have a very small increase.
function Ball:checkDistance(paddle)
local section = paddle.w / 5
local delta = self.h
-- x[_1_|_2_|_3_|_4_|_5_]w
--i need to check if the ball is close to the center, the further away the more we will alter the horizontal velocity
--this covers 1-5, the whole paddle, always true because thats how we got to this function
if self.x >= paddle.x and self.x <= paddle.x + paddle.w then
print("1-5")
delta = self.h * 1.75
--this covers 2-4 and overrides the prior
elseif self.x >= paddle.x + section and self.x <= paddle.x + paddle.w - section then
print("2-4")
delta = self.h * 1.45
--this covers 3 and overrides the two prior conditions
elseif self.x >= paddle.x + 2*section and self.x <= paddle.x + paddle.w - 2*section then
print("3")
delta = self.h * 1.15
end
self.h = delta
if self.h >= 225 then
self.h = 225
elseif self.h <= -225 then
self.h = -225
end
end
This never seems to evaluate in the 2-4 or 3 range, even my AI, which always trys to center the paddle onto the ball, can only return the "1-5." Am I completely blind and missing something obvious?
if 3 then ...
elseif 2-4 then ...
elseif 1-5 then ...
end
Why? Because if the "3" condition is true, the other two are true as well. And if the "2-4" condition is true, the "1-5" condition is true as well. That way, it makes sense you never get the bodies for the two narrower conditions the way you have it now.
if self.duration <= .1 * self.durationOrg then self.color = {255, 0, 0}
elseif self.duration <= .2 * self.durationOrg then self.color = {227, 31, 0}
elseif self.duration <= .3 * self.durationOrg then self.color = {199, 59, 0}
elseif self.duration <= .4 * self.durationOrg then self.color = {171, 87, 0}
elseif self.duration <= .5 * self.durationOrg then self.color = {143, 115, 0}
elseif self.duration <= .6 * self.durationOrg then self.color = {115, 143, 0}
elseif self.duration <= .7 * self.durationOrg then self.color = {87, 171, 0}
elseif self.duration <= .8 * self.durationOrg then self.color = {59, 199, 0}
elseif self.duration <= .9 * self.durationOrg then self.color = {31, 227, 0}
elseif self.duration <= self.durationOrg then self.color = {0, 255, 0}
end
So the idea here is that sure, 1-5 will be true, and then it'll apply the alteration i want accordingly and store it in a variable. If 2-4 is true then it will overwrite that variable, same with the center section. Do I need to reverse their orders? I do don't I....
For the first one? Yeah, you do. The thing you just showed is right, because the smallest range goes first. On the other hand, I would implement it myself with a table:
-- outside any function
local thingy_colors = {{255, 0, 0}, {227, 31, 0}, ...} -- etc.
-- instead of the grand if-statement:
if self.duaration <= self.durationOrg then
self.color = thingy_colors[math.floor(10*self.duration/self.durationOrg) + 1]
end
Untested, it might have an off-by-one error or something like that.
Robin wrote:For the first one? Yeah, you do. The thing you just showed is right, because the smallest range goes first. On the other hand, I would implement it myself with a table:
-- outside any function
local thingy_colors = {{255, 0, 0}, {227, 31, 0}, ...} -- etc.
-- instead of the grand if-statement:
if self.duaration <= self.durationOrg then
self.color = thingy_colors[math.floor(10*self.duration/self.durationOrg) + 1]
end
Untested, it might have an off-by-one error or something like that.
Yea that looks a lot nicer! I was, obviously, trying to work with a case statement work around. So just to be clear, elseifs don't get evaluated if the higher if/elseif does? Other than that I am marking this as solved. Thanks again!
then someFunction() will never be called because the moment Lua sees "true or " it already knows the statement as a whole is also true and stops evaluating.