[XNA] screencoordinates

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Post Reply
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

[XNA] screencoordinates

Post by King Harold »

Ok, I know this is a noob question.. but with 3D I'm a noob so, well..

How do you calculate the screen-coordinates of a 3d vector?

And then I mean exactly how, because I know that it probably "has something to do with multiplying your world, view, and projection matrices with it" but that's not exactly it.. or is it..?
User avatar
Halifax
Sir Posts-A-Lot
Posts: 225
Joined: Mon 01 Jan, 2007 10:39 am
Location: Pennsylvania, US

Post by Halifax »

I am answering this with only 3D experience, no XNA experience.

But usually aren't 3D vectors attached to the origin? Usually I have never seen them used for displaying anything on the screen, but more so for an intermediate data format for calculations.

Why do you need to display it on screen though?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Well, I have a 3D object that I want to stick a few sprites to..
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

A matrix represents a transformation of some sort (it could be translation, rotation, skewing, whatever you want really). You can combine matrices by multiplying them.

I'm not sure how you've structured your vertex shader and are setting the matrices, but assuming you're using world (moving an object to its location in the world), view (translating and rotating around the camera) and projection ("project" a 3D point to 2D and depth buffer information) then you can just multiply them together (and your point) and you'll get the screen position out (though to get absolute coordinates you'll need to scale and offset by the backbuffer dimensions).

The quick answer, however, is to look up billboarded sprites and render them normally in 3D rather than using a 2D workaround. (Matrix.CreateBillboard).
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Billboarding is a bit complicated though..
I'd rather let the SpriteBatch deal with the trouble and only have to calculate a few coordinates

So is that just
world * view * projection * point
and then do some magic to scale/offset it?
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

King Harold wrote:Billboarding is a bit complicated though..
I'd rather let the SpriteBatch deal with the trouble and only have to calculate a few coordinates
SpriteBatch is rather more complicated in my experience, as it tends to mess up renderstates. Using billboards is exactly what you seem to need to do; drawing 2D sprites in 3D locations.
So is that just
world * view * projection * point
and then do some magic to scale/offset it?
Should be, depends on how you're dealing with all the other vertices in your vertex shader. I'm not sure of the exact values output by the projection matrices that D3D generates, but I'm guessing they'll be normalised (in X and Y). You should be able to guess with a bit of debugging...
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

benryves wrote:
King Harold wrote:Billboarding is a bit complicated though..
I'd rather let the SpriteBatch deal with the trouble and only have to calculate a few coordinates
SpriteBatch is rather more complicated in my experience, as it tends to mess up renderstates. Using billboards is exactly what you seem to need to do; drawing 2D sprites in 3D locations.
It has a savestate option, I don't know how well it works though, but for what I'm doing it doesn't matter much really..
benryves wrote:Should be, depends on how you're dealing with all the other vertices in your vertex shader. I'm not sure of the exact values output by the projection matrices that D3D generates, but I'm guessing they'll be normalised (in X and Y). You should be able to guess with a bit of debugging...
I'll try it, but probably not today (have to finish reversi today, for Imperative Programming)
Post Reply