Jump to content

  • Log In with Google      Sign In   
  • Create Account


Moving an object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 martinradev   Members   -  Reputation: 143

Like
0Likes
Like

Posted 29 March 2014 - 05:09 AM

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?



Sponsor:

#2 Aardvajk   Crossbones+   -  Reputation: 5799

Like
3Likes
Like

Posted 29 March 2014 - 05:40 AM

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 :)).


Edited by Aardvajk, 29 March 2014 - 05:49 AM.


#3 martinradev   Members   -  Reputation: 143

Like
0Likes
Like

Posted 29 March 2014 - 06:26 AM

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



#4 IceCave   Members   -  Reputation: 157

Like
0Likes
Like

Posted 29 March 2014 - 02:23 PM

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.


Edited by IceCave, 29 March 2014 - 02:24 PM.


#5 warnexus   Prime Members   -  Reputation: 1395

Like
0Likes
Like

Posted 29 March 2014 - 03:51 PM


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?


Edited by warnexus, 29 March 2014 - 03:51 PM.


#6 martinradev   Members   -  Reputation: 143

Like
1Likes
Like

Posted 29 March 2014 - 04:00 PM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS