Hi, I’m new to Defold and I’m trying to do flappy bird game mechanics bird revolving around a planet and jumping similiar to this game https://play.google.com/store/apps/details?id=com.mrflap
I’m stuck with how to do gravity pull and revolving motion around planet at same time.
This is what I have done
function init(self)
msg.post(".", "acquire_input_focus")
self.velocity = vmath.vector3() -- <1>
self.gravity = vmath.vector3() --<2>
self.pos = vmath.vector3() --<3>
self.direction = nil --<4>
self.movement_direction = nil --<5>
self.ground_contact = false --<6>
self.center = go.get_position('/planet') -- <7>
self.speed = 4 -- <8>
self.t = 0 -- <9>
end
function final(self)
-- msg.post(".", "release_input_focus")
end
function update(self, dt)
self.radius = vmath.length(go.get_position('/planet') - go.get_position())
self.t = self.t + dt
local dx = math.sin(self.t * self.speed) * self.radius
local dy = math.cos(self.t * self.speed) * self.radius
local delta = go.get_position('/planet') - go.get_position()
self.direction = vmath.normalize(delta)
self.gravity = self.direction * 10
if not self.ground_contact then
self.velocity = self.velocity + self.gravity
go.set_position(go.get_position() + self.velocity * dt )
else
self.pos = go.get_position()
self.pos.x = self.center.x - dx
self.pos.y = self.center.y - dy
self.movement_direction = vmath.normalize(self.pos)
go.set_position(self.pos)
end
-- reset volatile state
self.correction = vmath.vector3()
-- self.ground_contact = false
end
-- from the platformer tutorial on defold.com
local function handle_geometry_contact(self, normal, distance)
local proj = vmath.dot(self.correction, normal)
local comp = (distance - proj) * normal
self.correction = self.correction + comp
go.set_position(go.get_position() + comp)
if normal.y > 0.7 then
self.ground_contact = true
end
proj = vmath.dot(self.velocity, normal)
if proj < 0 then
-- remove that component in that case
self.velocity = self.velocity - proj * normal
self.velocity.x = 0
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
if message.group == hash("planets") then
self.ground_contact = true
handle_geometry_contact(self, message.normal, message.distance)
end
end
end
function on_input(self, action_id, action)
if action_id then
-- jump
if action_id == hash("touch") then
if action.pressed then
self.ground_contact = false
self.velocity = - self.gravity * 30
-- reset()
end
end
end
end
function on_reload(self)
end
Any help would be much appreciated, thank you.