#35871 - mksoft_ - Fri Feb 11, 2005 7:12 pm
Hello
I've done a mode 7 background, it works fine, no problem with that.
Now i want to have 3d sprites, and i can't get this working, and i tried every tutorials on the web (which are great, but never give a working function without using extras .h or others libs...)
I try to have a function like that
void ComputeSprite(x,y,z, *screenX, *screenY, *scale)
{
...
}
IN:
x,y,z are the 3d coord of the object
OUT:
screenX, screenY are the 2d pos of the projected object
and scale the scale value for the sprite
could you help me please?
thank you thanks you thanks you
:)
#35889 - AnthC - Sat Feb 12, 2005 12:35 am
You could express the translation for a world co-ordinate to screen co-ordinate then pump your 3d position through this..
#35911 - mksoft_ - Sat Feb 12, 2005 11:02 am
Ok, here it is the part from the darkfader tutorial, some things i dont get:
Sprites
-------
Now you want to have some sprites positioned at some 3D coordinate...
This is done by transforming world coordinates to view coordinates and then project
them on the screen.
Subtract the camera coordinate from the world coordinate and then rotate with view matrix.
-> that's ok
Then you'll have to do an inverse transformation to get view space coordinates.
-> i do that, is it ok ?
pp.x = VF8_MUL(ctab[(255-yaw)]>>16, (p.x-camPos.x)) - VF8_MUL(stab[(255-yaw)]>>16, (p.z-camPos.z));
pp.y = p.y;
pp.z = VF8_MUL(stab[(255-yaw)]>>16, (p.x-camPos.x)) + VF8_MUL(ctab[(255-yaw)]>>16, (p.z-camPos.z));
Then optionally negate the Z coordinate if this is somehow negative and you don't want this.
Now you can check if it's behind you (Z < 0) and ignore it :)
-> i don't get at all, i made it positive and check if it's negative ??
Then you need to project it into the screen to get the right screen space coordinates.
This is done with a projection matrix, but since this is nothing more than scaling things nicely,
you only need to store 2 scale values according to the field-of-vision (FOV).
Define the x/y scaling for fast projection:
mproj_w = LCD_WIDTH/2 / tan(0.5 * FOV_horizontal)
mproj_h = LCD_HEIGHT/2 / tan(0.5 * FOV_vertical)
-> what value should have FOV_horizontal and FOV_vertical ? It depends of what?
Then... project!
screen_x = LCD_WIDTH/2 + in.x * mproj_w / in.z
screen_y = LCD_HEIGHT/2 - in.y * mproj_h / in.z
-> ok for that
After you have the screen coordinates, you have to shift the sprite a bit because unfortunately the rotation origin
is at the center, but not the offset.
Therefore you'll still need the Z coordinate. Shift by ?/z and set sprite scale to ?*z.
-> what is '?' ? What is the exact formula ?
Thanks for your help i'm really stuck and i need to finish very soon
pleeasse :)
#35935 - LOst? - Sun Feb 13, 2005 7:35 am
Quote: |
Shift by ?/z and set sprite scale to ?*z. |
Come on! Please! People listen up!
This is not help! This is not a tutorial! If some of you like a tutorial with just an explaination of a value that is not showed off anywhere, then it's the same thing as having DATA in a CPP file and iclude it directly in another CPP file like this:
Code: |
#include "data.cpp" |
#35938 - mksoft_ - Sun Feb 13, 2005 9:39 am
...
i'm sorry if i said something wrong, i didn't meant to.
I'm just in trouble seeking for help...
You don't have to help if you don't want...
#35963 - ecurtz - Sun Feb 13, 2005 6:19 pm
If you aren't using "real" 3d elsewhere in your code (and it doesn't sound like you are) it is probably simpler to use raycasting techniques than 3d transforms for your sprites.
Think about the overhead view of your world. You can calculate the angle from your (camera) origin to the sprite using atan2. You can calculate the distance to the sprite using the Pythagorean theorem.
The x position on screen is then determined by the difference of which angle your camera is facing and which angle the sprite is. The y position is based on your mode 7 projection - you want it to be as far up as the floor pixel which is the same distance away, then you add an amount based on the z value scaled by the same amount your sprite is scaled down (also based on how far away it is / your mode 7 values.)