Moving an object

Started by
4 comments, last by Martin Radev 10 years ago

Hello!

I have an issue moving an object in a 2d world. A little about the project:

I am building a game for which:

- the game is a top-down 2d shooter

- I create the map from a tmx file

- I have a "physics world" and a graphical world

- the "physics world" is just a collision detector and just some objects which I translate, rotate, etc. Nothing fancy. All was coded by me, but it works fine.

- the graphical world is represented by objects I just paint on the screen using the Java Graphics API

The big problem is the movement, translation, movement of camera.

Currently, I have come up with the solution that I will not move the main player (my player) in order to keep him on the screen. I move all other objects relatively to him.

However, now that I have implemented the "respawn logic", it is hard to move the translation offset to the new main player coordinates.

I am absolutely sure that there is a better solution and I would like you guys to tell me how you do it. Currently, the biggest problem is that I have to move 200 objects relatively to the main player coordinates. For now, it works smoothly, but I believe it is a bad idea. Also, everything is built from scratch and I do not use any frameworks like LibGDX.

Any suggestions?

Advertisement

Have all the objects positions defined in world space, including the player. Maintain a camera position. When you render, subtract the camera position from each object's worldspace position to get the object's view space position. As part of the player's update, move the camera so that the player's world position will come out as the centre of view space i.e. camera = player(x,y) - (screenwidth/2, screenheight/2).

You should only really modify positions when drawing. Moving objects conceptual world space positions around is a no-no (unless the objects are moving in the world of course :)).

I started like that, but I think I had some issues with the Java Graphics API. I will try to rewrite my movement.

Moving your objects relative to the origin of the camera is exactly what OpenGL and DirectX is doing.

Since your screen literally can't move, the objects position has to be moved relatively to your center of view.

This is one of the laws in graphic programming: Not the "camera" is moving but the whole world around it.

I guess your problem is caused by a coding decision. It should be totally fine by drawing like this


// abstract fantasy code
draw() {
     drawImageAt( image, x + Camera.X, y + Camera.Y );
}

And yes this slows down your code a little bit of course by doing this additional stuff but since you are using the java graphics library to draw, you had to be totally aware that you cannot make a game with thousands of objects on screen with this. (In OpenGL and DirectX neither but this is another topic)

However those are just additions and calculated in a few ticks by your CPU that shouldn't be a problem.


And yes this slows down your code a little bit of course by doing this additional stuff but since you are using the java graphics library to draw, you had to be totally aware that you cannot make a game with thousands of objects on screen with this.

How come not 1000 objects? I'm using the Java API and in my game I got 9 monsters to render at once. Not saying I cannot get more than 9 monsters to render but given the size of map, 9 was more appropriate.

I remember reading somewhere getting 60 players in a AAA FPS game was a massive challenge. Is the obstacle necessarily about the game calculating too many things per frame which prevents the game from renders 1000 objects?

Ok, I got it working! Now, I just give some offset to the screen and it works pretty smoothly. Now, I was also able to improve the other logic / code.

I am fully looking forward for OpenGL, but I have left it for the summer since I also plan to switch to C++ from Java and this requires a little more time as well.

Thank you for the help!

This topic is closed to new replies.

Advertisement