[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

06/14/2009 at 02:21PM PDT, ID: 24490751
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.6

How can I improve my picking precision?

Asked by Astynax777 in DirectX Graphics & Game Programming, Game Programming, 3D Game Programming

Tags: SlimDX, DirectX 9, Direct3D9, 3D, Picking, Worldspace, Screenspace, DirectX SDK (March 2009)

So I've decided to mess around with SlimDX, which is a great C# library for utilizing DirectX in a managed environment. It can be found at http://slimdx.mdxinfo.com/ .  I'm using the release complimenting the DirectX SDK (March 2009) release.  I had the most difficult time getting picking to work because my card world matrix was not invertible.  I solved that and got picking working however it's not as precise as I like.  If I move the camera out further, then my range of selecting a card becomes more narrow.  I created a bounding box in my Card class which I will post following this and everything seems correct to me.  Could anyone please give me some pointers as to how I could improve the precision of my pointing?  Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
 
using SampleFramework;
using SlimDX;
using SlimDX.Direct3D9;
 
namespace CSSlimDX
{
    class CSSlimDX : Game
    {
        const int InitialWidth = 1024;
        const int InitialHeight = 768;
        
        Card [] cards;
        public Card selectedCard = null;
        Camera camera = new Camera();
        Light light = new Light();
        TextConsole console = new TextConsole();
        
        public Device Device
        {
            get { return GraphicsDeviceManager.Direct3D9.Device; }
        }
 
        public Color ClearColor
        {
            get;
            set;
        }
 
        public CSSlimDX()
        {
            ClearColor = Color.FromArgb(0, 45, 50, 170);
 
            Window.ClientSize = new Size(InitialWidth, InitialHeight);
            Window.Text = "CSSlimDX - Test";
            Window.KeyDown += new KeyEventHandler(Window_KeyDown);
            Window.MouseClick += new MouseEventHandler(Window_MouseClick);
            
            DeviceSettings settings = new DeviceSettings();
            settings.BackBufferWidth = InitialWidth;
            settings.BackBufferHeight = InitialHeight;
            settings.DeviceVersion = DeviceVersion.Direct3D9;
            settings.Windowed = true;
            settings.EnableVSync = true;
            settings.MultisampleType = MultisampleType.EightSamples;
 
            camera.FieldOfView = (float)(Math.PI / 4);
            camera.NearPlane = 1.0f;
            camera.FarPlane = 1000.0f;
            camera.Location = new Vector3(0.0f, 0.0f, -200.0f);
            camera.Target = Vector3.Zero;
 
            Resources.Add(console);
 
            //GraphicsDeviceManager.ChangeDevice(DeviceVersion.Direct3D9, true, InitialWidth, InitialHeight);
            GraphicsDeviceManager.ChangeDevice(settings);
 
            Device.SetRenderState(RenderState.ZEnable, true);
            
 
            light.Type = LightType.Directional;
            light.Direction = new Vector3(0.0f, 0.0f, 1.0f);
            light.Diffuse = Color.FromArgb(200, 200, 200);
            light.Ambient = Color.FromArgb(200, 200, 200);
 
            Material material = new Material();
            material.Diffuse = Color.White;
            material.Ambient = Color.White;
            Device.Material = material;
            
            Device.SetRenderState(RenderState.Lighting, true);
            Device.SetLight(0, light);
            Device.EnableLight(0, true);
            
        }
 
        void Window_MouseClick(object sender, MouseEventArgs e)
        {
            if (selectedCard != null)
            {
                selectedCard.locked = !selectedCard.locked;
            }
        }
 
        protected override void Initialize()
        {
            base.Initialize();
            
            cards = new Card[13];
            cards[0] = new Card(Device, CardList.BLUE1);
            cards[0].camera = camera;
            cards[1] = new Card(Device, CardList.BLUE2);
            cards[1].camera = camera;
            cards[2] = new Card(Device, CardList.BLUE3);
            cards[2].camera = camera;
            cards[3] = new Card(Device, CardList.BLUE4);
            cards[3].camera = camera;
            cards[4] = new Card(Device, CardList.BLUE5);
            cards[4].camera = camera;
            cards[5] = new Card(Device, CardList.BLUE6);
            cards[5].camera = camera;
            cards[6] = new Card(Device, CardList.BLUE7);
            cards[6].camera = camera;
            cards[7] = new Card(Device, CardList.BLUE8);
            cards[7].camera = camera;
            cards[8] = new Card(Device, CardList.BLUE9);
            cards[8].camera = camera;
            cards[9] = new Card(Device, CardList.BLUE10);
            cards[9].camera = camera;
            cards[10] = new Card(Device, CardList.BLUE11);
            cards[10].camera = camera;
            cards[11] = new Card(Device, CardList.BLUE12);
            cards[11].camera = camera;
            cards[12] = new Card(Device, CardList.BLUEWILD);
            cards[12].camera = camera;
            
        }
 
        protected override void LoadContent()
        {
            base.LoadContent();
 
            camera.AspectRatio = (float)Window.ClientSize.Width / Window.ClientSize.Height;
            
            float positionX = (float)cards.Length / 2 * -5;
            //int positionX = 0;
            float positionZ = 3;
            foreach (Card card in cards)
            {
                card.OnResetDevice();
                card.position.X = positionX;
                card.position.Y -= 50;
                card.selectedPosition = card.position;
                //card.position.Z = positionZ;
                card.selected = false;
                Components.Add(card);
                positionX += 5;
                positionZ += 1;
            }
        }
 
        protected override void UnloadContent()
        {
            base.UnloadContent();
 
            foreach (Card card in cards)
            {
                card.OnLostDevice();
            }
        }
 
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            
            foreach (Card card in cards)
            {
                card.Dispose();
            }
 
        }
 
        void Window_KeyDown(object sender, KeyEventArgs e)
        {
            // F1 toggles between full screen and windowed mode
            // Escape quits the application
            if (e.KeyCode == Keys.F1)
                GraphicsDeviceManager.ToggleFullScreen();
            else if (e.KeyCode == Keys.Escape)
                Exit();
        }
 
 
        protected void CheckMousePicking()
        {
            foreach (Card card in cards)
            {
                card.selected = false;
            }
 
            
            Point cursorPos = Window.PointToClient(Cursor.Position);
            int mouseX = cursorPos.X;
            int mouseY = cursorPos.Y;
            
            // Clamp mouse coordinates to viewport 
            if (mouseX < 0) mouseX = 0;
            if (mouseY < 0) mouseY = 0;
 
            if (mouseX > this.Device.Viewport.Width) 
                mouseX = this.Device.Viewport.Width;
            if (mouseY > this.Device.Viewport.Height) 
                mouseY = this.Device.Viewport.Height;
 
            
            Vector3 near = new Vector3(mouseX, mouseY, 0f);
            Vector3 far = new Vector3(mouseX, mouseY, 1f);
 
            Vector3 position;
            Vector3 direction;
 
            Ray transformedRay;
            bool found = false;
            for (int i = cards.Length - 1; i >= 0 && !found; i--)
            {
                // Transform points to world space 
                Matrix foo = Device.GetTransform(TransformState.Projection);
                Matrix temp = cards[i].worldMatrix;
                
                position = Vector3.Unproject(near, this.Device.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, cards[i].worldMatrix);
                direction = Vector3.Unproject(far, this.Device.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, cards[i].worldMatrix);
                
             
                transformedRay = new Ray(position, direction);
                
                float distance;
                if (Ray.Intersects(transformedRay, cards[i].box, out distance))
                {
                    cards[i].selected = true;
                    selectedCard = cards[i];
                    found = true;
                }
                else
                {
                    cards[i].selected = false;
                    selectedCard = null;
                }
            }
        }
 
        
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
 
            CheckMousePicking();
            
        }
 
        protected override void Draw(GameTime gameTime)
        {
            Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, ClearColor, 1.0f, 0);
            Device.BeginScene();
 
            Device.SetTransform(TransformState.View, camera.ViewMatrix);
            Device.SetTransform(TransformState.Projection, camera.ProjectionMatrix);
 
            base.Draw(gameTime);
 
            console.Begin();
            console.Location = new Point(5, 5);
            console.ForegroundColor = new Color4(1.0f, 1.0f, 1.0f, 0.0f);
            console.WriteLine(GraphicsDeviceManager.DeviceInformation);
            console.WriteLine(GraphicsDeviceManager.DeviceStatistics);
            console.WriteLine(gameTime.FramesPerSecond.ToString(".00", CultureInfo.CurrentCulture));
            
            console.End();
 
            Device.EndScene();
        }
    }
}
[+][-]06/14/09 02:25 PM, ID: 24625242

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/14/09 04:18 PM, ID: 24625542

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: DirectX Graphics & Game Programming, Game Programming, 3D Game Programming
Tags: SlimDX, DirectX 9, Direct3D9, 3D, Picking, Worldspace, Screenspace, DirectX SDK (March 2009)
Sign Up Now!
Solution Provided By: Astynax777
Participating Experts: 0
Solution Grade: A
 
 
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625