• Advertisement

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 5442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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


KaMiKaZe

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
I''m assuming here, that now you have vPickRayDir and vPickRayOrig, you want to find a point (X,Z) where Y = 0 (ground level).


  
float ddy = vPickRayOrig.y / (vPickRayDir.y * -1.0f);
vPoint2D = vPickRayOrig + (vPickRayDir * ddy);


vPoint2D now contains a coord, (X, 0.0, Z).

Cheers

Matt

Share this post


Link to post
Share on other sites
***wow*** matt..., it worked!!!
thanks!

ehm.., could you explain a bit what happened? also, what''s with the -1.0f?

and what should i look out for using this formula? (ie, shortcomings etc.?)

thanks again!

Share this post


Link to post
Share on other sites
Drawback: It assumes you are always looking down onto the Y0 plane

To break it down..
vPickRayDir contains the direction AND the magnitude of the pick ray.

The Y portion of that vector (as it''s always looking downwards) is negative; * -1.0f, turns it to a positive number.

Dividing the vPickRayOrig.y with that positive number tells us how far down the direction vector to travel to hit Y=0 (length "ddy").

eg,
vPickRayOrig.y = 30.0f;
vPickRayDir.y = -250.0f;

ddy = 30.0 / (-250 * -1)
ddy = 0.12

So, we travel from the origin, in the direction of the vector- but only part way down it accoring to ddy;

vPoint2D = vPickRayOrig + (vPickRayDir * ddy)

Just solving for Y you''ll see that;
30.0 + (-250 * 0.12)
= 0.0

(sorry, but I''m not always that good at writing explainations!)



Help me!

Share this post


Link to post
Share on other sites
yeah thanks a lot matibee!

any idea what to do if ground isn''t aligned in y0 anymore? or how to do it at arbitrary height? or this is way out of scope(ie, involves a lot of computation )

Share this post


Link to post
Share on other sites
Hrm. The following code (spliced together from the pick sample and my 3D math book) works when the camera is looking straight down the z-axis; however, when the camera is rotated, it doesn''t. (What it looks like it''s doing, near as I can figure, is failing to take the rotation into account...)

Can any of you see what I''m doing wrong? I sure can''t.

-- Odd the Hermit


  
// Mostly from the Pick Poly sample.

D3DXVECTOR3 v;
D3DXMATRIX matProj, m;

// Get the projection matrix.

g_pDevice->GetTransform( D3DTS_PROJECTION, &matProj );

v = D3DXVECTOR3( (( 2.0f * m_ScreenPos.x / g_DeviceWidth ) - 1 ) / matProj._11,
(( 2.0f * m_ScreenPos.y / g_DeviceHeight ) - 1 ) / matProj._22,
1.0f );

// Get the view matrix and invert it.

g_pDevice->GetTransform( D3DTS_VIEW, &m );
D3DXMatrixInverse( &m, NULL, &m );

D3DXVECTOR3 vecPickOrig = D3DXVECTOR3( m._41, m._42, m_43 );
D3DXVECTOR3 vecPickDir = D3DXVECTOR3( v.x*m._11 + v.y*m_21 + v.z*m._31,
v.x*m._12 + v.y*m._22 + v.z*m._32,
v.x*m._13 + v.y*m._23 + v.z*m._33);

// Intersect the ray with the plane the mouse is on (parallel to the xy plane).

D3DXVECTOR3 vecNormal(0.0, 0.0, -1.0 );
float t = -(D3DXVec3Dot(&vecNormal, &vecPickOrig) +m_WorldPos.z) / D3DXVec3Dot(&vecNormal, &vecPickDir);

m_WorldPos.x = vecPickOrig.x + t*vecPickDir.x;
m_WorldPos.y = -( vecPickOrig.y + t*vecPickDir.y );

Share this post


Link to post
Share on other sites

  • Advertisement