Jump to content

How to get the distance traveled by a sprite ?

Recommended Posts

Hi folks !


I'm making a game with Phaser and I have some space ships.
My ships are firing bullets (thanks to http://gamemechanicexplorer.com/ Seriously, I love you man <3).
My map is 4800px in width and 2400px in height.
For now, the bullets are traveling all the map untill they find a side of the world.


What I would like to do is to set a range for my ships, something like 250px. So I think the solution is to kill my bullets after X pixels traveled (in my case X = 250). I'm thinking of calculate this distance traveled with the velocity at each frame but I'm not sure about that.

I can't find any online solution, so maybe someone can help me here.


Thank you guys !
And sorry for my poor english (don't hesitate to correct my sentences :))

Link to post
Share on other sites

A time based solution might be a decent idea, just kill them after a set amount of time, have that time dictated by velocity and you'll be able to work out a range for them, although, if you're updating a 'life' variable each tick you might as well just add velocity and when it hits a certain threshold, bye bye bullet

Link to post
Share on other sites

You can use sqrt( (ship.x - bullet.x)^2 + (ship.y - bullet.y)^2 ) to get the distance between the ship and a bullet. Check this each frame for each bullet and kill the bullet if the calculated distance is greater than 250.

The other option is to set a lifetime on your bullet, all sprites have this I believe. After the given time it is automatically destroyed. Just figure out how fast a bullet is traveling per second and make sure this add up to 250 or whatever you thing is proper.

Third option is to check if the bullet is in the camera-bounds. I think sprites have a method for this as well. That way bullets will never vanish while in view of the player, but also not travel around the gameworld where the player cannot see it.

Link to post
Share on other sites

Thanks guys !


I think that the distance between the ship and the bullet is a nice idea. Even if it's not exactly correct because if the ship turns back the bullet will die quicker than if the ship just follows the direction of his bullet.


But it's an easy solution and I think it will do the job. I will use the game.physics.arcade.distanceBetween(ship, bullet) method.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...