\$49

### Image of the Day Submit

IOTD | Top Screenshots

## Shooting in 3D world where mouse clicked

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1neogeocg  Members

Posted 17 December 2012 - 12:45 PM

Hello,
I am making this 3rd person shooting game in which there is a player and enemies in the 3D world and I want to shoot arrows at the enemies.What I want is to control the cross hair of the player via the mouse.The problem is mapping mouse coordinates to 3D world.Now I am well aware of the popular picking code at MSDN forum but that is for the First Person Camera.Mine is a Chase camera(the camera is positioned at some offset behind the player).I cant seem to understand how to give the proper direction and initial position of arrow so far I have used this but it fails.
Vector3 farSource = new Vector3(Mouse.GetState().X, Mouse.GetState().Y, 1);
Vector3 farPoint = Game1.Instance.GraphicsDevice.Viewport.Unproject(farSource, camera.Projection, camera.View, Matrix.Identity);
Game1.Instance.ModelManager.addModel("Arrow", (ModelPosition + new Vector3(0, 40, 0)), Vector3.Normalize(farPoint-Vector3.Normalize(ModelPosition+new Vector3(0,40,0)))*2.0f);

Basically what I am trying to do is when the user presses right click on the mouse, a model of arrow is added to the 3D world whose initial position is that of player position plus some offset(so that shot is fired from the centre of player) and the direction is determined by subtracting far point from player position and then normalizing it.This is not working because shots are very offset from original mouse position.Please help.Thank you

### #2GeniusPooh  Members

Posted 18 December 2012 - 12:57 AM

hmm

this is a queit question for everyone..

I saw same quesiton at graphics section..

Hmm to find where I click on 3D space.

you must check what point I click on view frustum's near plane and far plane.

if you know 4 corner point or near plane you can assume where is my mouse.

Match screen to near plane. screen position is like 0~ 1024 but near plane is come from actual position ..

Hmm... It's too simple

plz give me more question for understanding..It's not easy at first step

Beauty is only skin deep , ugly goes to bones

World's only 3D engine tunner and 3D engine guru.

and real genius inventor but very kind warm heart .. and having serious depression for suffering in Korea

www.polygonart.co.kr ( currently out dated and only Korean will change to English and new stuff when I get better condition sorry for that)

### #3SpeedRun  Members

Posted 18 December 2012 - 08:02 AM

You can check this tutorial I had written to map screen coordinates to world coordinates. You might need to tweak it but the underlying concept should work for you

##### Screen To World Coordinates
Last Update Dec 04 2012 05:40 AM

### #4neogeocg  Members

Posted 18 December 2012 - 09:24 AM

first of all thanks for your reply GeniusPooh.I am sorry I didn't get all of it.I mean I am already unprojecting mouse positions at at both near and far plane in 3D world.what I want is to change the near point to player's position.I can't seem to get that working.As you can see I have tried to translate the near point to player's position and then calculated the direction of arrow from near near point to far point.But it still not working.Where am I wrong?

### #5neogeocg  Members

Posted 18 December 2012 - 09:25 AM

Thank you speedRun I will check it

### #6Suspense  Members

Posted 18 December 2012 - 06:18 PM

Ok, you're normalizing the direction vector, right? But when you calculate the direction, it looks like you're subtracting a normalized vector from an unnormalized vector. Have you tried it without that inner call to normalize? Unproject should return a point in world space, and your model coord should also be in world space, so I suspect that normalize call is messing you up.

In simpler words, try something like this:
Game1.Instance.ModelManager.addModel("Arrow", (ModelPosition + new Vector3(0, 40, 0)), Vector3.Normalize(farPoint-(ModelPosition+new Vector3(0,40,0)*2.0f)));


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.