• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# i have pickrayorig and pickraydir, how do i know how far my click is?

This topic is 5768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, ie, dungeon siege, neverwinter nights, how do i move my character to the spot click by the mouse, i have the rays from the sdk sample, thanks,

##### Share on other sites
D3DXVECTOR3 clickspot = rayorigin + (raydirection * raylength)

You already know the rayorigin + raydirection... The ray function to determine if it hits anything will return the length of the ray if it does hit something.

If you know the clickspot you know how to make the guy move from its current location to the clickspot right? Its almost 2d at that time.

##### Share on other sites
I dont think it''s working this way. :D Length of the ray is useless. I''m working on the same problem. You need to know how far you clicked from your character''s previous position right?

If you can figure something out let me know :D

##### Share on other sites
hi JoeyBlow2,

i think me and dracoon have exactly the same problem,

you see your formula is correct,

clickspot = rayorigin + (raydirection * x)

where x is the value the character should go to.., but how do calculate for x? or probably it''s up to us to implement a collision detection?

##### Share on other sites
quote:
Original post by mickey
hi JoeyBlow2,

i think me and dracoon have exactly the same problem,

you see your formula is correct,

clickspot = rayorigin + (raydirection * x)

where x is the value the character should go to.., but how do calculate for x? or probably it''s up to us to implement a collision detection?

If i understand correctly then you can simply rearrange for x:

x = (clickspot - rayorigin) / raydirection

##### Share on other sites
Use "D3DXIntersectMesh()", it will give you a distance value.

KaMiKaZe

##### Share on other sites
we don''t know x and clickspot,

##### Share on other sites
I may place one mesh as the ground. It''s a simple square from two triangles. Then D3DXIntersect returns the barycentric u and v coords.
----
|1/|
|/2|
----

1st triangle
Upper Left corner 0,0
Upper Right corner 1,0
Bottom Left corner 0,1

combined with the 2nd triangle (using "1-")
Bottom Right corner 1,1

I can multiply the value with the square''s width so I got the exact coords. I also know the character''s previous position, so I can simply calculate the angle and the distance. I can also move and zoom and rotate the camera freely.

Just an idea, I will try it tomorrow. What do you think?

##### Share on other sites
Maybe another way with D3DXIntersect, if I can set the Picking Ray''s origin to the character, and the direction to the point I clicked. But I dont even fully understand the sample''s coords...

D3DXMATRIXA16 matProj;
pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
D3DXVECTOR3 v;

v.x=(((2.0f*ptCursor.x)/Width)-1)/matProj._11;
v.y=-(((2.0f*ptCursor.y)/Height)-1)/matProj._22;
v.z=1.0f;

D3DXMATRIXA16 matView, m;
pd3dDevice->GetTransform( D3DTS_VIEW, &matView );
D3DXMatrixInverse( &m, NULL, &matView );

vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;

Do you? :D

##### Share on other sites
my ground isn''t an x file, i generated it at runtime,

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634076
• Total Posts
3015353
×