Captain’s log No.2, 2021-03-05T23:23:00Z:
P0N6 AKA: Pong
I’ve been working on this for about three days before making a topic on this forum, so I’m logging my progress here.
T+3days:
I created this project.
I ran through the files, checking out what they had to offer and reacquainted myself with Defold and the different files available.
I set up inputs in input/game.input_binding for WASD, Arrow keys and Controller and edited the game.project file a bit.
I drew simple shapes and set up game object files for Pong.
T+2days:
I started work on the scripts. Programmed the paddles first making them actually move. Referencing APIs, Manuals and Examples from Defold Learn I slowly built my code, excessively studying it if it failed. Then the ball.
ball.script
function init(self)
--[[excerp from the code]]--
math.randomseed(os.time())
math.random();math.random();math.random(); --clear consistent RNG buffer
local dir = math.random(0, 360) --set initial direction
while dir == 90 or dir == 270 do
dir = math.random(0, 360)
end
dir = math.rad(dir)
self.speed = 1000
self.vel = vmath.vector3(self.speed * math.cos(dir), self.speed * math.sin(dir), 0)
end
This is what the code looks like now. It used to be a lot more messy, until I figured out velocity is a vector. I used to define the ball’s velocity with a speed and a directional angle. That means that my update(self, dt)
function handled velocity calculating each movement separately using math.sin()
and math.cos()
.
That worked until I had to program the ball “reflecting” (same angle in, same angle out) of the walls. See, me using the angle to move meant I had to make separate calculations for each of the four quadrants the ball was oriented towards, leaving me with clunky code. Now, well, it’s much simpler:
function reflect(self) --linear relfection of ball on out of bounds
self.vel.y = 0 - self.vel.y
end
After reflecting the ball off the walls (see details^) I added the ability for the ball to bounce off the paddles. Since I switched to a civilised vector math, it was easy. I dove into the on_message(self, message_id, message, sender)
function and message_id = "contact_point_response".
I wanted to make the bounce directional based on where on the paddle the ball lands, instead of the boring constant angle, giving the game some strategy. So, I calculate the excentricity of the hit:
ball.script#bounce()
function on_message(self,message_id,message,sender)
local pad = message.other_position
local bal = message.position
local where = bal.y - pad.y --where on paddle: excentricity of the hit to paddle's center
local touch = where / 70 --ratio excentricity:half_paddle_size =~ excentricity [%]
bounce(self,touch)
end
function bounce(self, touch) --directional bounce off paddles; touch: where on the paddle the ball touches
local dirX = 0
if self.vel.x > 0 then --direction left_right
dirX = -1
else
dirX = 1
end
local dir = math.rad(75) * touch --y rotational direction based on paddle hit location
self.vel.x = math.cos(dir) * dirX * self.speed
self.vel.y = math.sin(dir) * self.speed
end
T+1day_yesterday:
I deleted the bit of code within ball.script
that restricted the game object between y=50 and y=700 and replaced it with another game object that had a sprite and collision object. After I remembered to set it to “static” and after a long and painful process of trouble-shooting leading to me also remembering what masks on a collision object are for, I was set to start. And boy was it painful.
For some reason my reflect(self)
code seemingly broke. I figured since the physics object sends out multiple events, I should make sure it reflects once, thus saving the last border under self.lstBrdr=message.other_id,
since the ball can’t hit the same border twice. Nothing resolved the issue and all the code seemed fine.
Long story short, turns out the if not
command works under very specific conditions in Lua that I hadn’t realised.
if not self.lstBrdr == message.other_id then --> breaks everything
if not (self.lstBrdr == message.other_id) then -->works flawlessly
Yup… Two parenthesis made all the difference.
I also set up a simple score-keeping gui, that changes based on how many times the paddles have hit the ball and also changes colour based on which was last.
…once I figured out the msg.post(id, message_id, message)
function, of course, or rather the addressing bit and how to write an id
in the first place.
T-0_current:
This is the state of it right now:
You are now up-to-date
End Log, 2021-03-06T00:30:00Z