Page 1 of 1

[Solved]Pong:velocity change based on dist. from center

Posted: Mon Jul 22, 2013 3:56 am
by Shanulu
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.

Code: Select all

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?

Re: Pong:horizontal velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 9:09 am
by Robin
Yeah.

What you have now:

Code: Select all

if 1-5 then ...
elseif 2-4 then ...
elseif 3 then ...
end
What you need:

Code: Select all

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.

Re: Pong:horizontal velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 3:38 pm
by Shanulu
The was the idea... I was trying to implement it similar to my blocks color change

Code: Select all

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

Re: Pong:horizontal velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 4:38 pm
by Robin
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:

Code: Select all

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

[SOLVED]Re: Pong:velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 5:36 pm
by Shanulu
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:

Code: Select all

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

Re: Pong:horizontal velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 7:40 pm
by Plu
Nope. It's all lazy; as soon as something pings as true, none of the rest is done.

Even goes for the inside of ifs by the way, if you do this:

Code: Select all

if true or someFunction() then
end
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.

Re: Pong:horizontal velocity change based on dist. from cent

Posted: Mon Jul 22, 2013 8:15 pm
by Robin
You could see it like this:

Code: Select all

if a then
  doX()
elseif b then
  doY()
elseif c then
  doZ()
end
is basically the same as:

Code: Select all

if a then
  doX()
end
if not a and b then
  doY()
end
if not a and not b and c then
  doZ()
end