Simulated 3D and scaling into Distance??
Hi I’m trying to simulate 3D on a very Limited Device.
There is no support for floating point and very limited math’s libraries. (Micro Java)
Also the proc of the device is pretty slow!
The screen is 95pixles wide and 99pixels high.
What I am trying to do is create a 3D room, and move an object around this room.
The room will be created by using a background to simulate the cube and the object will be moved about over this back ground and its pixel position will be adjusted and the image changed (a smaller image will be used) to simulated the object moving into the distance.
The Dimensions of the cube are 90 * 90 * 90
Although the z-axis might need to be adjusted, as this is not important exactly what length it is as a different length might make the calculations easier!
The front of the cube will range
x from pixels 0 to 90
y from pixels 0 to 90
the rear of the cubes dimensions will range pixels from
x from pixels 30 to 60
y from pixels 30 to 60
and the magic number is 45 (i think) as no matter how far down the z axis say (1/3) then the scaled x axis will range from 10 to 80 and half way is 45 the number will not stop re-occurring as i was trying to work out how to position the image as it moves along the z axis.
I''m finding this difficult to articulate so hopefully your following or can anticipate where i am stuck.
If the objects position is
x = 9
y = 12
z= 90 ( its as far away as possible maybe not 90 ifs its a bad number to use)
object needs to be positioned not at pixel 9,12 but translated
to 9 / 3 = 3 + 30xPixels = screen position 33x
12 / 3 = 4 + 30yPixels = screen position 34x
so that it appears in the correct position in the distance!
I have been wracking my brain for ages now trying to figure away to come up an algorithm that will perform this translation.
I know an array of translations would work but it seems so inefficient!
Can anybody see wtf i am missing ;-(
Thanks :-D
July 18, 2003 05:45 PM
resolution....???
Which would be 95 * 99
but if the part of the screen been used be the game is 90 * 90 what happens then?
Thanks
Which would be 95 * 99
but if the part of the screen been used be the game is 90 * 90 what happens then?
Thanks
ScreenX = ZoomFactor * WorldX / WorldZ + ScreenPosFactorX
ScreenY = ZoomFactor * WorldY / WorldZ + ScreenPosFactorY
Explanation:
The above formulas give you the position on the screen (ScreenX, ScreenY) at which a point in 3D space (WorldX, WorldY, WorldZ) should appear. ZoomFactor is a number, usually fairly large, that determines your "field of view", or the angle you can see to either side of the line defined by WorldX = 0 and WorldY = 0 (the direction you''re looking). ScreenPosFactorX and ScreenPosFactorY just shift the range of screen coordinates as required so they are centred where you want them on the screen.
It''s easiest to think of your 3D world space as going from -45 to 45 in the X and Y dimentions instead of 0 to 90. The range of world Z values harder to determine, and is somewhat linked to the ZoomFactor value.
You want the range of motion of the object to correspond to 90 pixels when it''s as close as it can get to the camera, and 30 pixels when it''s as far away as it gets. 90/30 = 3, so the back of your cube should be 3 times as far away in world space as the front. If you want to maintain the 90 worldspace units depth of the cube, this requires solving the following equations:
Zmax - Zmin = 90
Zmax = Zmin * 3
Zmin * 3 - Zmin = 90 = Zmin * 2
Zmin = 45
Zmax = 135
So your world Z values should range from 45 to 135.
Next you need to figure out the ZoomFactor such that the range of x and y world coordinates from -45 to 45 map to the screen coordinates -45 to 45.
Going back to the original equations, and putting 0 for the ScreenPosFactorX (which just shifts the image around on the screen as needed, so doesn''t affect the size of the range of possible screen coordinates)
ScreenX = ZoomFactor * WorldX / WorldZ + ScreenPosFactorX
-45 = ZoomFactor * -45 / 45
ZoomFactor = 45
And, lastly, you need the image to be located from 0 to 90 pixels, not -45 to 45. Thus, make ScreenPosFactorX and ScreenPosFactorY 45
So, you end up with
ScreenX = 45 * WorldX / WorldZ + 45
ScreenY = 45 * WorldY / WorldZ + 45
Which, I hope, will map the above specified range of world coordinates to the 0 to 90 horizontal and vertical range on the screen. If you want to display actual images at these locations, you may need to limit the world coordinate range slightly more, as if your image is 20 pixels wide, and it''s centred at (0, 0), then the top and left 3/4 of the image will be off the screen.
If you don''t like using -45 to 45 for the range of WorldX and WorldY, and 45 to 135 for WorldX, you can add / subtract constants to the actual ranges of these values to convert them from "0 to 90" before putting them into the formulas for ScreenX and ScreenY.
If someone else would like to check that over, that''d be good. It''s been a while since I''ve transformed world to screen coordinates on my own.
ScreenY = ZoomFactor * WorldY / WorldZ + ScreenPosFactorY
Explanation:
The above formulas give you the position on the screen (ScreenX, ScreenY) at which a point in 3D space (WorldX, WorldY, WorldZ) should appear. ZoomFactor is a number, usually fairly large, that determines your "field of view", or the angle you can see to either side of the line defined by WorldX = 0 and WorldY = 0 (the direction you''re looking). ScreenPosFactorX and ScreenPosFactorY just shift the range of screen coordinates as required so they are centred where you want them on the screen.
It''s easiest to think of your 3D world space as going from -45 to 45 in the X and Y dimentions instead of 0 to 90. The range of world Z values harder to determine, and is somewhat linked to the ZoomFactor value.
You want the range of motion of the object to correspond to 90 pixels when it''s as close as it can get to the camera, and 30 pixels when it''s as far away as it gets. 90/30 = 3, so the back of your cube should be 3 times as far away in world space as the front. If you want to maintain the 90 worldspace units depth of the cube, this requires solving the following equations:
Zmax - Zmin = 90
Zmax = Zmin * 3
Zmin * 3 - Zmin = 90 = Zmin * 2
Zmin = 45
Zmax = 135
So your world Z values should range from 45 to 135.
Next you need to figure out the ZoomFactor such that the range of x and y world coordinates from -45 to 45 map to the screen coordinates -45 to 45.
Going back to the original equations, and putting 0 for the ScreenPosFactorX (which just shifts the image around on the screen as needed, so doesn''t affect the size of the range of possible screen coordinates)
ScreenX = ZoomFactor * WorldX / WorldZ + ScreenPosFactorX
-45 = ZoomFactor * -45 / 45
ZoomFactor = 45
And, lastly, you need the image to be located from 0 to 90 pixels, not -45 to 45. Thus, make ScreenPosFactorX and ScreenPosFactorY 45
So, you end up with
ScreenX = 45 * WorldX / WorldZ + 45
ScreenY = 45 * WorldY / WorldZ + 45
Which, I hope, will map the above specified range of world coordinates to the 0 to 90 horizontal and vertical range on the screen. If you want to display actual images at these locations, you may need to limit the world coordinate range slightly more, as if your image is 20 pixels wide, and it''s centred at (0, 0), then the top and left 3/4 of the image will be off the screen.
If you don''t like using -45 to 45 for the range of WorldX and WorldY, and 45 to 135 for WorldX, you can add / subtract constants to the actual ranges of these values to convert them from "0 to 90" before putting them into the formulas for ScreenX and ScreenY.
If someone else would like to check that over, that''d be good. It''s been a while since I''ve transformed world to screen coordinates on my own.
Thanks Geoff thats perfect!
Tested it out with pen and paper and it works great!
BUT......
I have 1 last hurdle to jump !
ScreenX = 45 * WorldX / WorldZ + 45
ScreenY = 45 * WorldY / WorldZ + 45
if x = 30 and z = 70
ScreenX= 45 * 30/70 + 45
= 45 * .428571 + 45
= 19.285714 + 45
= 64.285714
Using Micro Java there is no support for doubles or floats!
Is there a work around that will still give you the answer!
ScreenX = 62 would be accurate enough!
the main problem that i see is with WorldX/worldZ as this always returns a fraction!
Thanks Brian
Tested it out with pen and paper and it works great!
BUT......
I have 1 last hurdle to jump !
ScreenX = 45 * WorldX / WorldZ + 45
ScreenY = 45 * WorldY / WorldZ + 45
if x = 30 and z = 70
ScreenX= 45 * 30/70 + 45
= 45 * .428571 + 45
= 19.285714 + 45
= 64.285714
Using Micro Java there is no support for doubles or floats!
Is there a work around that will still give you the answer!
ScreenX = 62 would be accurate enough!
the main problem that i see is with WorldX/worldZ as this always returns a fraction!
Thanks Brian
If there is no support for floating point, then in multiplication/division combos, save your division for last.
45 * 30/70 + 45
1350 / 70 + 45
19.286 + 45
rounded: (19) + 45
64
If you do the division first:
45 * 30/70 + 45
45 * 0.4286 + 45
rounded: 45 * (0) + 45
0 + 45
45
oops!!!
So just write the code so that the division happens last.
Perhaps this will work:
ScreenX = (45 * WorldX) / WorldZ + 45
ScreenY = (45 * WorldY) / WorldZ + 45
[edited by - Waverider on July 19, 2003 10:55:06 AM]
45 * 30/70 + 45
1350 / 70 + 45
19.286 + 45
rounded: (19) + 45
64
If you do the division first:
45 * 30/70 + 45
45 * 0.4286 + 45
rounded: 45 * (0) + 45
0 + 45
45
oops!!!
So just write the code so that the division happens last.
Perhaps this will work:
ScreenX = (45 * WorldX) / WorldZ + 45
ScreenY = (45 * WorldY) / WorldZ + 45
[edited by - Waverider on July 19, 2003 10:55:06 AM]
It's not what you're taught, it's what you learn.
Micro Java doesn''t support divisions? I assume it would have just rounded the answer to an integer.
It's not what you're taught, it's what you learn.
Yes thats true!
but rounding .4245 to 1 will not make the equasion work!
I cant come up with a work around but people have managed to simulate 3D with MicroJava so they''re must be away!
Cheers
but rounding .4245 to 1 will not make the equasion work!
I cant come up with a work around but people have managed to simulate 3D with MicroJava so they''re must be away!
Cheers
quote:
Original post by Woody FX
rounding .4245 to 1 will not make the equasion work!
Read what Waverider wrote more carefully:
quote:
Original post by Waverider
If there is no support for floating point, then in multiplication/division combos, save your division for last.
Youre not rounding .4245 to 1. You never need to. You do the multiplication FIRST:
quote:
Original post by Waverider
( <--- notes by me )
45 * 30/70 + 45
1350 / 70 + 45 <--- NOTE!!!! Multiplication Evaluated FIRST
19.286 + 45 <--- Rounding Required, but not .4245 to 1
rounded: (19) + 45
64 <--- Good Integer approximation
Make a quick test program to check if the integer division rounds correctly. I think it probably will.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement