XNA Game Development Forums
2012/05/18 06:11:06 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Question about Getting 2D screen coordinates  (Read 2462 times)
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« on: 2007/02/23 08:58:57 »

I've read through the "picking" tutorial and that's all pretty cool, however, I need to do the opposite, effectively.

I'm writing a space-flightsim and I want to be able to display a targetting bitmap around whichever ship is currently targeted (using conventional 2D sprite methods).

Is there a straightforward way of getting a 3D Objects' actual 2D screen co-ordinates?

Any help greatly appreciated.
Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #1 on: 2007/02/23 09:14:19 »

multiply its position by the world view projection matrix Smiley
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #2 on: 2007/02/23 16:13:32 »

can you give an example please mike?

I.e. store the result in what??

Thanks.
« Last Edit: 2007/02/23 16:23:19 by DaphydTheBard » Logged
mikeschuld
Administrator
Sr. Member
*****
Offline Offline

Posts: 389


View Profile WWW
« Reply #3 on: 2007/02/25 13:08:10 »

Vector3 screenCoords = Vector3.Transform(obj.Position, obj.World * camera.View * camera.Projection);

Wink
Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #4 on: 2007/02/26 06:01:53 »

Mike

I couldn't get your method to work properly, however I've found another way which works:

Here's how to do it, for anyone interested.  Obviously the function needs tweaking for your own engine to pass in matrixes and viewport sizes.  However, this code works a treat:

public static Point Convert3DPointTo2D(Vector3 point, XNAViewPort objVP)
        {           
            Vector4 result4 = Vector4.Transform(point,
                objVP.MatrixView * objVP.MatrixProjection);
           
            if (result4.W == 0)
                result4.W = 0.000001f;
            Vector3 result = new Vector3(
                result4.X / result4.W,
                result4.Y / result4.W,
                result4.Z / result4.W); 
           
            return new Point(
                (int)Math.Round(+result.X * (800 / 2)) + (800 / 2)+100,
                (int)Math.Round(-result.Y * (600 / 2)) + (600 / 2)+100);
        }

I can't take credit for this code as it was modified from the rocket commander source code  Wink
« Last Edit: 2007/02/26 06:33:31 by DaphydTheBard » Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #5 on: 2007/02/26 06:44:07 »

I have had a pop at this too, and thanks to DaphydTheBard's example I came up with this:

Code:
        public Point Get2DCoords(GameWindow window)
        {
            HMCameras.HMCamera camera = HMCameras.HMCameraManager.ActiveCamera;
            Matrix ViewProjectionMatrix = camera.View * camera.Projection;

            Vector4 result4 = Vector4.Transform(myPosition, ViewProjectionMatrix);
            if (result4.W == 0)
                result4.W = HMHelper.Epsilon;

            Vector3 result = new Vector3(result4.X / result4.W, result4.Y / result4.W, result4.Z / result4.W);

            return new Point((int)Math.Round(+result.X * (window.ClientBounds.Width / 2)) + (window.ClientBounds.Width / 2), (int)Math.Round(-result.Y * (window.ClientBounds.Height / 2)) + (window.ClientBounds.Height / 2));
        }

I placed the method in the HMObject class so that I can get the 2D coords of all my objects. I am starting to feel like a code troll! You guys pop the ideas up there and I nick em  Embarrassed Maybe I should start having ideas of my own.....

[EDIT]
Just goes to show how important QA is, testing this on my poop laptop seemed to be OK, but once I got home and gave it a go I altered it to the one you now see here in this post....  Embarrassed It's a good job I don't have ideas of my own, I might do some dammage....
[/EDIT]
« Last Edit: 2007/02/27 12:35:09 by Nemo Krad » Logged
DaphydTheBard
Jr. Member
**
Offline Offline

Posts: 55


View Profile
« Reply #6 on: 2007/02/27 03:23:36 »

Nothing wrong with using other peoples' code - we all do it!  Grin

I think the important thing is to understand the code if it's "borrowed".  Wink
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Tutorial 6 :: Getting a Model on Screen Tutorial Discussion mikeschuld 11 5582 Last post 2007/02/18 10:35:28
by Iarus
Screen Flickering problem... Hazy Mind XNA Engine simpson.jon 4 1699 Last post 2007/02/25 17:09:05
by simpson.jon
Question: Texturing Terrain General Discussion Ashe 1 2336 Last post 2007/03/17 10:28:36
by DaphydTheBard
Question about getting rotation angles from Quaternions... General Discussion DaphydTheBard 7 3131 Last post 2007/03/20 07:27:20
by DaphydTheBard
Array Question General Discussion LucianX 11 3350 Last post 2007/04/13 04:51:19
by Nemo Krad
Coordinates General Discussion « 1 2 3 » LucianX 36 8954 Last post 2007/03/28 08:20:27
by muchrejoicing
Tutorial 3 help... new question i believe Hazy Mind XNA Engine hansonc 9 3126 Last post 2007/05/15 01:32:01
by BackwardsBoxers
tutorial 3 sprite question Hazy Mind 3D Engine precious roy 2 2728 Last post 2009/10/18 08:53:11
by Wekbaite73
FPS question General Discussion XNASorcerer 2 1468 Last post 2007/04/28 15:23:19
by XNASorcerer
HMObject design question Hazy Mind XNA Engine jdowling 1 1266 Last post 2007/05/11 23:05:38
by Tiago
Thanks And A question Hazy Mind XNA Engine zachaller 3 1567 Last post 2007/06/19 03:36:22
by Nemo Krad
Question About Packing Maps into one file? General Discussion jaypaul 0 1560 Last post 2009/06/03 05:12:47
by jaypaul
Adding in a Screen Manager Hazy Mind XNA Engine mimminito 1 1683 Last post 2010/02/20 23:59:09
by Jeff Lamoureux
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.971 seconds with 19 queries.