Sign in to follow this  
neogeocg

Shooting in 3D world where mouse clicked

Recommended Posts

neogeocg    111
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.
[b]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);[/b]
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

Share this post


Link to post
Share on other sites
GeniusPooh    161
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

Share this post


Link to post
Share on other sites
SpeedRun    850
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

[sharedmedia=blog:entries:2255516]

Share this post


Link to post
Share on other sites
neogeocg    111
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?

Share this post


Link to post
Share on other sites
Spencer Bowers    449
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:
[CODE]
Game1.Instance.ModelManager.addModel("Arrow", (ModelPosition + new Vector3(0, 40, 0)), Vector3.Normalize(farPoint-(ModelPosition+new Vector3(0,40,0)*2.0f)));
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this