2D Image

Started by
7 comments, last by Nik02 14 years, 1 month ago
Hello I am fairly new to game programming and need some advice on 2d Image Rendering.. I am working on PSP Platform..for a 2d Top-Down scrolling game... I have written a camera system which will be actually attached to the player..so as the player moves from top to down the camera will follow him... but as the user player and AI Player images i have rendered is in pixels...they get rendered flat on the screen...now even if i move the camera the images that i have rendered do not move and as they are getting rendered in pixels which is irrespective of the camera position... what i am trying is attaching my camera to the player and the Ai Players have a constant position initially but later they move too...so my camera always follows my player..i guess i have to render the images using co-ordinate positions(X,Y,0) rather that pixels(X,Y)... Images rendered in pixels do not move as i move my camera....should i use Quad.. if so how do i animate the image... please advice...this is my first game...as to what method can i use.. you can imagine an example for MARIO...
Advertisement
i guess things are not that clear from the above post so i am elaborating it more..

well i am trying to move the user player and the rest of the things in the game are stationary..at a fixed place...

so when the player moves the rest of the things move backward...but now the images are just clinged to the screen so even when i move the camera they do not move as they are given pixel positions on the screen...
A camera is just an offset. You draw objects at their position, translated by the camera offset. In 3D, a camera does a little more than just translating things, but the principle is the same. You transform world coordinates into screen coordinates. Of course, you have to actually perform this transformation.

For example, if sprite A is located at position (10, 20), you would normally draw it on the screen at (10, 20). However, if your camera is moving 30 pixels/units to the right, the sprite should be drawn at (10 - 30, 20). So, the screen coordinate of a sprite is it's world coordinate minus the camera offset. In pseudo-code:
screenCoordinate.x = worldCoordinate.x - camera.xscreenCoordinate.y = worldCoordinate.y - camera.y


You can perform this translation yourself, as shown above, or you can push a translation matrix on the stack if you're using an API that works like that - the idea is the same. Whether you're using quads or not doesn't matter.
Create-ivity - a game development blog Mouseover for more information.
hi Captain P

thanks for the reply...well i have a few more queries...

1. I have set my camera relative to the world co-ordinate at (0,0,0)
2. now I have my 2d player image rendered in pixels at (100,90)

now suppose i move my player in y direction from (100,90) to suppose (100, 120)

then how should i give the new position of player(which is in pixels) to the camera which takes values in co-ordinates...as i have move the camera along with the player...

This is where i am stuck...


Are you sure that PSP is a wise platform choice for a game development beginner?

Anyway, pixels are just an unit of measurement for coordinates in this context. What units of measurement is the camera position defined in? If the player position and camera position are expressed in different units, you have to convert either of them to match the other.

That said, it is a bad idea to use different units of measurement for things that should be in same logical space.

Niko Suni

i know psp is not a good choice for a beginner...we are just exploring various options in it and learning...this is one of them...

so if i should not use different co-ordinates then as it is side scrolling game then what is the best option should i use to make a scrolling game...

my requirement is that player jumps from a ship...there are 10 AI players along with him...after the player jumps..after some AI players jumps too randomly...

there were two options with me..
1. to move the background..(to simulate jumping and falling)
2. move the camera (move the player and camera follows)

my images were rendered in pixels so was my background...so moving the background was easy...but then i had to move the other images which i rendered along with the background...(like a space ship, 10 AI players all getting moved along with the screen, and then after some time the AI players also jumped after the player jumps)

so camera became the obvious choice...as it will be following the player so anything that came in the camera view will be seen...so i would not have to bother moving the other images in the game..along the screen...

I am hopeful there must be some way...


Yes, camera following your main character is the best option for 2D top down.

For example, if your camera is 50 pixels to left of your character, and 30 pixels up, and have area of 100x60, I'd do this:

cameraX = characterX - 50 + (playerBitmapWidth/2);
cameraY = characterY - 30 + (playerBitmapHeight/2);

That way, it will always have your character centered exactly in middle, and it will follow the character around.
In practice, games that have a "camera" (ie. every game except puzzlers) do actually move all the drawable objects.

If the camera is at [10,10], you offset the rendering of all your visible objects inversely, thus [-10,-10]. The "camera" concept is just a convenient encapsulation to store this offset. You don't have to move the internal coordinates of all the visible objects, just draw them with the offset.

In 3D, the concept is exactly the same: after transforming your meshes to their world-space location using a world matrix (to place them in your "world"), you transform them by the view matrix which is actually the inverse of the "camera" position and location.

Niko Suni

Also, XNA is an infinitely better platform for beginner game dev, as you get robust tools for development as well as debugging. In contrast, it is very cumbersome to debug running code on PSP (I don't even know if it is at all possible to debug break on homebrew PSP).

The game logic part is very similar to implement regardless of the platform, though.

Niko Suni

This topic is closed to new replies.

Advertisement