[libGDX] Translating worldcoordinates to screencoordinates for projectiles

Hi,

I’ve got a vertical scrolling map where I created a player and i wanted to have it shoot projectiles.
I’m getting the projectile’s start X&Y coordinates with getX()/getY() from my player class which extends libgdx’s Sprite class.

Thing is this passes the world coordinates instead of the screencoordinates from my player to my projectile Class.
Now when i want to draw my projectiles on screen the starting Y coordinate is the players Y-screencoordinate plus the amount the map has already scrolled. Which is not what i want ofc, because now the projectiles starting point moves further and further away from the player with every scoll update.

Do you have a tip how I could handle this problem.
As i’m writing this i’m thinking about creating a variable that keeps track of how much the map already has scrolled and substract this from the Y-worldcoordinate the player is passing to the projectile when shooting. This could be one way to solve my issue.

But maybe there is an easier way??

You can watch this video. I am too lazy to explain :stuck_out_tongue:

mehhh FYI:

i Solved my problem pretty easily by adding a variable scolltracker to my handleScroll method & in my render method subtract the projectile’s Y coordinates with this variables.
If anyone else has another idea, it would be nice to know too

private void handleScroll(float delta) {
cam.translate(0, SCROLLSPEED * delta, 0);// xf* delta sets the
// scrollspeed
scrollTracker += SCROLLSPEED * delta;
}

Maybe it’s just me but your post doesn’t make any sense. You should be able to spawn and render your projectile at your world coordinates (just as you do with your player), you shouldn’t be using screen coordinates for that. :persecutioncomplex:

Yeah i saw that one. Nice video, i llike his tutorials… but it didnt really give me insight in how to fix my issue.
Well solved it now…

@Pandamonium:
Yeah thats how i wanted to do it, but my player class extends Sprite and is being passed the OrthogonalTileMapRendererer (mainly so it can check the tilemap’s layer for tiles it collides with)
I’m then drawing my player sprite to to spritebatch of my OTMRenderer & drawing it in my game class’ render method.
(i used some code out of a tutorial)

I then tried to use the same kind of code logic for my projectile class, but i just couldn’t get it to work (nullpointer exceptions one of the issues f.e.) and it also didnt make to much sense to me to pass the OTMRenderer. It just seemed like making everything to difficult.

So i decided to make a more simple projectile class and draw my projectile on a differnet spritebatch (not the OTMrenderer’s one) but then ran into this issue

If you’re getting NullPointerExceptions then something is not good there.
Instead of hacking your way around the problem (which can cause issues later anyway) you should fix it.
Also the “took some code of a tutorial” mentality is never good, instead of copying code you should learn the theory and apply what you’ve learnt to solve your own problems. :point:

Edit: By the way what is that OTMRenderer that you keep talking about?

@Panda (or whomever would like to comment):
(btw. Forgot to explain, but OTMRenderer = OrthogonalTileMapRenderer)

I ran into a problem trying to render & spawn my projectiles in the same way as i did with my player.
When i create my player class it extends Sprite and i pass it a Sprite and a TileMapTileLayer for checking collision.
That works fine, but with my projectiles i wanna create an Array that stores my projectiles & the Array(list) isn’t applicable for these arguments.
Atm I dont really see how I should solve this problem.

Any recommandations would be appreciated