PDA

View Full Version : about the z-axis in 2D



OMG
July 14th, 2010, 11:56 PM
hi
I'm making a game where the area contains 3 dimensions. A ball can move left and right (x axis), bounce in the air (y axis) and up and down (z axis), like in a 2d scrolling game. Imagine a fighting game like street fighters, but the ppl can move up and down too.

the problem is that I have no idea how to simulate the z axis. For example, I have two balls, initially at the same position, that move to the right with an angle of 20 degree between them (on the z axis. So in a normal perspective the balls are still sticking on the ground but in a flash simulation we see that one ball is going up and one ball is going down).

if it were just a simple simulation I could go lazy and directly change the x and y axis to create the same effect, but in this project I need to know a way (maybe a formula) to actually control the z axis and to adjust the balls' y position according to it. Any help?

Thanks.

Broadsmile
July 17th, 2010, 02:28 PM
You need imagination and maths. In 3D games You place a camera and how You see objects depends on angles between each of them and the camera.

Basically, the camera is in middle, that makes perfect sense. So:

camera.x = stage.width/2;
camera.y = stage.height/2;
camera.z = 0;

//Z, X, Y axis distance between object and the camera
zCamObj = ball.z - camera.z;
xCamObj = ball.realx - camera.x;
yCamObj = ball.realy - camera.y;

if (zCamObj <0) ball.visible = false; //behind camera

ball.x = camera.x + xCamObj/zCamObj;
ball.y = camera.y + yCamObj/zCamObj;


The further a ball is, the more it is in the middle. Look at the moon. It's perfectly in front of You. now move Yourself one step to Your left. You moved by ~ 40 centimeters, but the moon looks like it didn't move (relatively to You) at all. The further an object is, the less it (virtually) moves by x and y axises.

But now another exercise (:D) - if an object is 50 pixels on right of the camera, and 1 pixel away (zCamObj = 1), and now You move it one pixel further (zCamObj ++), the ball.x changes from 250 to 200+50/2=225 ! It moved 25 pixels left because moving it 1 pixel further! It can't be right!

No, it can. Someone wrote in some tutorial some time ago, that You shouldn't try to simulate physics in computer game, because You will end with very unrealistic gameplay. That's a good advice for an *** from physics. But when You're good at physics and Maths, then You will have essential imagination to do realistic, physical animations... So! 1 pixel z difference means that the object is very very very close to the camera. Moving it 1 pixel further is a big difference. Imagine looking at cube, in which some balls are flying around and bounce from it's walls. Now, if You look from a distance of 1 milimeter, and some ball will bounce from the wall You're looking through, the effect would be similar. To avoid that, set boundaries to right Z values, like between 100 and 300 (I didn't test it!)

Of Course cameras have different lens and You may want to alter the view by adding some factors You multiple values by.

Maybe I forgot about something, maybe I wrote something wrong, but I think You get the point and can experiment by Yourself. Good luck.

EDIT: Oh, I would forgot. If You want to scale the ball too (yea, that's important effect), You can either multiple some fixed factor by zCamObj, or - if You're a perfectionist - apply above algorithm to top-left, and bottom-right corners of objects, instead of to centers.

OMG
July 18th, 2010, 01:17 AM
wow that sure helped, thanks 8D