2
\$\begingroup\$

I'm building a simple 2D Top-Down shooter game in Java.

I simply cannot get this to work: I'm moving the camera and world differently. The world is moving when needed (when not in one edge, so it gives nice effect of not showing outside of the world). When the player moves and edges are not visible, the camera is moved when the player moves.

I implemented bullets right now, but the bullets move with the camera, not the world, like the correct physics should be :D

For moving the world relative to player position and camera, I'm using X and Y offsets.

Now the problem: I'm creating a new bullet like this in my Player.java class:

new Bullet(handler, angle, x + width / 2 - handler.getCamera().getxOff(), y + height / 2 - handler.getCamera().getyOff(), 2, 4);

Bullet constructor:

public Bullet(Handler handler, double rad, double x, double y, double speed, int r)

I have to add half the width and height of the player, because otherwise it will shoot at the corner of my player sprite. Camera X and Y offsets to the bullet X and Y, because otherwise the bullet will be drawn to the coordinates relative to the world and will not shoot from correct places. But when the bullet is moving it moves with the camera.

Really wouldn't like to change the Camera movement!

\$\endgroup\$
10
  • \$\begingroup\$ I think the problem probably lies in whatever update or tick function you have on your bullet, so if you could share that that would be great. \$\endgroup\$
    – elaid
    Jul 30, 2017 at 21:18
  • \$\begingroup\$ My bullet tick() method: public boolean tick() { x += dx; y += dy; if(x < -r || x > handler.getWidth() + r || y < -r || y > handler.getHeight() + r) { return true; } return false; } \$\endgroup\$
    – Kerdo
    Jul 30, 2017 at 21:42
  • \$\begingroup\$ If the tick method returns true, the bullet will get deleted. It's a removal if the bullet gets out of bounds. \$\endgroup\$
    – Kerdo
    Jul 30, 2017 at 21:43
  • 3
    \$\begingroup\$ Well why are you talking about the camera when you construct bullets? Right now you're constructing the bullet at a different place depending on where teh camera is. Obviously that's a problem. \$\endgroup\$
    – user253751
    Jul 31, 2017 at 0:18
  • 1
    \$\begingroup\$ Also, the world never moves. The camera does, so you should just make the camera move, instead of both the camera and the world (that just doesn't make sense to me). \$\endgroup\$
    – clabe45
    Aug 2, 2017 at 14:44

0

You must log in to answer this question.

Browse other questions tagged .