Hey everyone! Just wanted to share some code I made. I’m very new to lua and defold so I want to be able to give back anything that I can to the community.
I am not sure if someone else has figured this out already, but in my game, I needed to give a game object a direction and have it move in that direction.
For example, 90 degrees would be something like
function init(self)
xspeed = 1
yspeed = 0
end
function update(self, dt)
go.set(".", "position.x", (go.get(".", "position.x") + xspeed)
go.set(".", "position,y", (go.get(".", "position.y") + yspeed
end
And 180 degrees would be
xspeed = 0
yspeed = 1
But what about 45 degrees? You would think because its going towards the top left of the screen, it would be xspeed = 1 yspeed = 1 but that is incorrect because then whenever it travels towards the corners, it will travel faster than it would be if its going to the edges. How do we fix this? And how do we calculate all 360 degrees?
This problem took me a long time to figure out. After crawling the forums for hours, I found others with the same problem. Their solution was to use vectors, but I didn’t know how to use vectors because I didn’t understand quaternions. After a few hours on youtube trying to figure out quaternions, I figured, there must be a simpler solution for such a simple problem. All i want is the ball to move in a direction and the correct speed!
So… I asked one of my good friends what he thought. He’s good at math and he had a solution which was;
Cut the angles in to four quadrants, and manipulate each quadrant differently.
After a few hours of math later, I created this script:
go.property("ballrotprop", 0)
function init(self)
xspeed = 0
yspeed = 0
ballrot = self.ballrotprop
end
function update(self, dt)
--Makes 361 degrees = 1 degree
if ballrot >= 360 then
ballrot = ballrot - 360
end
if ballrot < 0 then
ballrot = ballrot + 360
end
--This code changes the angle of the ball
if ballrot >= 0 and ballrot <= 90 then
--Quadrant 1
xspeed = ballrot / 90
yspeed = 1 - xspeed
elseif ballrot >= 91 and ballrot <= 180 then
--Quadrant 2
xspeed = 1 - ((ballrot - 90) / 90)
yspeed = (1 - xspeed) * -1
elseif ballrot >= 181 and ballrot <= 270 then
--Quadrant 3
xspeed = ((ballrot - 180) / 90) * -1
yspeed = -1 - xspeed
elseif ballrot >= 271 and ballrot <= 359 then
--Quadrant 4
xspeed = (1 - ((ballrot - 270) / 90)) * -1
yspeed = 1 + xspeed
else
print("Quadrant Error!")
end
--This code makes the game object move in the direction of the angle
go.set(".", "position.x", (go.get(".", "position.x") + xspeed)
go.set(".", "position,y", (go.get(".", "position.y") + yspeed
end
For my needs, this code worked perfectly. If you want to just copy and paste it into your project, I believe it will work. If you want to make it turn, just change the value of ballrot during the game.
I’ll explain how the code works:
Everything inside the function init(self), is just establishing the variables and their default values. The go.property just makes it easier to test if its working or not.
The code that says “–Makes 361 degrees = 1 degree”, this just makes it so you never get a value above 359 or below 0. It also sets 360 to equal 0 (straight up)
The code that says “–This code changes the angle of the ball”, this code is split into four parts which correspond to these four quadrants (relative to the game object):
^
4 | 1
|
x <------------->
|
3 | 2
y
All that this code is doing is manipulating the xspeed variable so as to set it to the correct value, then it sets the yspeed value to whats left in the quadrant. In some quadrants, we desire the direction to be the mirrored result of previous quadrents, so thats why we multiply it by -1 and manipulate each quadrant differently.
Well that is my story guys! I’m using this code in a recreation of the Atari game “breakout”. I’ll make sure to share it with everyone when its finished.
This seems like a great community and I’m excited to keep learning more about lua and defold!