3D to 2D coordinates

Started by
5 comments, last by Koobazaur 17 years, 1 month ago
Hi there folks, I'm trying to mix some 2d and 3d but having a hard time transfering my 3d coordinates to screen coordinates. My resolution is 800x600, and I'm drawing pretty much all characters as sprite and the background also, but all other elements are in 3D (it's a platformer). When I draw sprites, the drawing coordinates is in screen coordinates, so what I want to do is e.g. draw a block then when the character hit is, it shatters but for that I need to get the screen coordinates of the 4 vertices of that block so I can check hit. How do you guys do this stuff, because in my book (introduction to 3d game... by Frank Luna), he uses a technique called Picking wich I used the code to do the inverse of switching mouse coord to 3d coord... but I dosent work. Any ideas ?
Advertisement
Moved to For Beginners.
Why not just use the view + projection matrix and multiply it by the vertices?

Another solution that, imho, would work better for the game overall is to have everything in 3D; that is, the sprites would actually be 3D objects walking on your land, but clipped so they can only move left/right and jump. Then, when you draw them, you can simply disable the depth buffer and the sprite will render without being obstructed by any of your 3d geometry.

Lastly, I am learning D3D9 from the same book ;)
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
This topic has come up more than a couple of times in the last two days in the DirectX forum. If you're using Direct3D, you may want to mosey on in that direction. Or I could save you the time and tell you to check out the D3DXVec3Project function.

If you're using OpenGL, then the technique is the same, but you don't have D3DX to do all the legwork for you. The idea is to create the world-screen transformation matrix (matViewport * matProjection * matView * matWorld) and transform the screen-space vector accordingly.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Thanks for you answer guys. I'll check up that D3DXVect3Project, seams like what I wanted =) And Koobazaur, I thought of doing that, but first what I thought to draw a plane rectangle than load the sprite as a texture on it, but found a problem when I wanted to animate it... When drawing sprites I can just specify a rect destination for my blit, wich makes it pretty easy and fast. Where you thinking of something else than that ?
If you did want to do simple x,y,z to x,y without matrices it goes like this:

x = x/z*F;
y = y/z*F;

where F should be the field of view, generally 60 is a good number but if you want to change it you can. 45 is a bit extreme sort of fish-eyed at a close distance and larger numbers mean you can see more.

Don't forget to divide by a floating point number, not an int... That is if you're storing your coordinates as int which you shouldn't.

That response addresses the most basic question of your title, however, you should look into the suggestions already brought forth. This is more for interest's sake.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
I was thinking about that. You can still specify a blit rectangle with textures; what you need to do is have one big texture of your animation (loaded from file) and a second texture attached to your in-game square (created in the program). Then you get the surfaces of both textures and load the part of your animation from the big texture onto your square texture, just like you would do with sprites.

Check out Direct3DTexture9::GetSurfaceLevel() and D3DXLoadSurfaceFromSurface().

You can find an easy to follow (albiet poorly documented) example at code sampler.
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...

This topic is closed to new replies.

Advertisement