Hi,
I am trying to implement a simple Asteroids game and a bit stuck with the implementation of the situation when a ship flies off the screen and it should then fly in to the screen but from the opposite location. Here is the screen shot which shows the ship off screen exit area and where I expect it to fly in. It seems like when I move horizontally it’s easy to just have some condition in update like if pos.x < 0 let say - set pos.x to screen.width x - but this only works with horizontal 90 degrees move when I start flying diagonally I need to calculate x and y depending from the x and y of the ship exist area. Basically I am following this tutorial and trying to re-implement the same with Defold https://www.youtube.com/watch?v=VIc8Kon5PFw&feature=youtu.be&t=623 and here is Game Maker function to do it GameMaker Manual
Is there any Defold example how to achieve the same results?
here is the code
go.property("angular_velocity", 1)
go.property("linear_velocity", 50)
function init(self)
msg.post(".", "acquire_input_focus")
self.input = vmath.vector3()
self.rotate = 0 -- direction of rotation (0 = no rotation, 1 = left, -1 = right)
self.accelerating = false
self.accelerate_impulse = 0
end
function update(self, dt)
-- handle rotation from key input left/right
local rotation = go.get_rotation()
if self.rotate ~= 0 then
rotation = rotation * vmath.quat_rotation_z(self.angular_velocity * self.rotate * dt)
go.set_rotation(rotation)
end
-- move in direction of rotation
local pos = go.get_position()
if self.accelerate_impulse > 0 and not self.offscreen then
local distance = self.linear_velocity * dt * self.accelerate_impulse
local direction = vmath.rotate(rotation, vmath.vector3(0, distance, 0))
pos = pos + direction
go.set_position(pos)
end
-- keep moving while holing acceleration button
if self.accelerating then
self.accelerate_impulse = self.accelerate_impulse + 0.02
else
if self.accelerate_impulse > 0 then
self.accelerate_impulse = self.accelerate_impulse - 0.003
end
end
-- when off the screen show in opposite side
if pos.y < -31 or pos.x < -31 or pos.x > 516 or pos.y > 516 then
self.offscreen = true
else
self.offscreen = false
end
if pos.x < -31 then
go.set_position(vmath.vector3(516, pos.y, 0))
elseif pos.x > 516 then
go.set_position(vmath.vector3(-31, pos.y, 0))
end
if pos.y < -31 then
go.set_position(vmath.vector3(pos.x, 516, 0))
elseif pos.y > 516 then
go.set_position(vmath.vector3(pos.x, -31, 0))
end
end
function on_input(self, action_id, action)
if action_id == hash("up") then
if action.released then
self.accelerating = false
else
self.accelerating = true
end
end
if action_id == hash("left") then
self.rotate = action.released and 0 or 1
elseif action_id == hash("right") then
self.rotate = action.released and 0 or -1
end
end
the screen size 500x500 that’s why there are magical 516 nunbers and the ship sprite is 32 px