Player Tracking Projectile(SOLVED)

Guys im not the biggest Trigonometry fan but I’m trying to convert my platformer game from Java. How can I convert this to lua based code? It is a projectile that fires at the players last position when it was spawned.

Ignore the reactionToCollision function its not relevant to the problem plus the redundancy in creating a new playerPosition :joy: (just noticed it)

I have not tested this code, but something like this:

function init(self)
  self.velocity = go.get_position("/player") - go.get_position()
  if vmath.length(self.velocity) > 0 then
    self.velocity = vmath.normalize(self.velocity)
  end
  self.removetimer = 2 -- seconds!
end

local SPEED = 400 -- pixels per second

function update(self, dt)
  -- update position
  local new_position = go.get_position() + self.velocity * SPEED * dt
  go.set_position(new_position)
  -- timer
  self.removetimer = self.removetimer - dt
  if self.removetimer <= 0 then
    go.delete()
  end
end
3 Likes

I’m actually working on it closely following the Java based version right now and ill post it when im done. Thank you though.

Note that you can do additions, subtractions and multiplications on the vectors directly. There is no need to calculate per component.

-- Instead of writing:
local new_pos = vmath.vector3()
local pos = go.get_position()
new_pos.x = pos.x + self.velocity.x * SPEED * dt
new_pos.y = pos.y + self.velocity.y * SPEED * dt

-- you can do:
local new_pos = go.get_position() + self.velocity * SPEED * dt
2 Likes

Ive noticed. Its just a habit coming from Java :joy:

:slight_smile:

Noticed that I forgot a check if velocity is >zero before normalizing. Edited code above.

1 Like

Got it working thank you. Just a curious question about tile maps. Does the collision get simplified combining each tile as one collision object or does it remain as individual squares(etc) at run time specified in the tilesource?

I have a flying enemy that follows the player around and shoot the projectiles at him and I set a condition to not fire if its colliding(using collision_response) with the tiles. I notice it only works when the enemy is at the outer tiles but still fires when its deeper and the projectile get removed at the outer tiles as well
SCENARIO 1


SCENARIO 2

Yep, there’s issues with tilemap collisions when entirely inside a tilemap. This was recently posted about by @andreas.strangequest and assigned a ticket.

1 Like