Jump to content
  • Advertisement
Sign in to follow this  
Meshboy

Help with C++ From C#

This topic is 3555 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

Hello, i have programmed with C# way to many years and forgotten about C++ and DirectX. My Method works in C# but not in C++ DirectX.I am new to DX C++, can anyone help me translate this to C++? C# WORKING ---------------- Vector3 AP = mPos - p1; Vector3 AB = p2 - p1; float ab2 = AB.X * AB.X + AB.Y * AB.Y + AB.Z * AB.Z; float ap_ab = AP.X * AB.X + AP.Y * AB.Y + AP.Z * AB.Z; float t = ap_ab / ab2; if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f; Vector3 Closest = p1 + AB * t; return Closest; ---------------- C++ DX (NOT WORKING) ------------------ D3DXVECTOR3 ClosestPoint(D3DXVECTOR3 mPos) { D3DXVECTOR3 AP; D3DXVECTOR3 AB; D3DXVec3Subtract(&AP,&mPos,&p1); D3DXVec3Subtract(&AB,&p2,&p1); float ab2 = AB.x * AB.x + AB.y * AB.y + AB.z * AB.z; float ap_ab = AP.x * AB.x + AP.y * AB.y + AP.z * AB.z; float t = ap_ab / ab2; if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f; D3DXVECTOR3 Closest; D3DXVec3Add(&Closest,&p1,&AB); D3DXVECTOR3 result = Closest * t; return result; } ------------------ Please help. DX C++ Is more difficult in many ways than C#, but its more fun and faster!

Share this post


Link to post
Share on other sites
Advertisement
Looks like order of operations to me - the single expression in C# multiplies, then adds, and the two expressions in C++ mean that you add first, then multiply the result; IE:

Vector3 Closest = p1 + AB * t;

vs.

D3DXVec3Add(&Closest,&p1,&AB);
D3DXVECTOR3 result = Closest * t;

or equivalently, (p1 + AB) * t

Share this post


Link to post
Share on other sites
Well, in the C++ version, you're doing this:

t * (p1 + AB)

instead of

p1 + AB * t

Also, the lines where you calculate ab2 and ab_ap can be reduced to the dot product of two vectors (ab dot ab for ab2, and ab dot ap for ab_ap).

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Your C# code should work as C++ with the single change of replacing "Vector3" with "D3DXVECTOR3".


No, you can´t use - and + when using Vectors in C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Well, in the C++ version, you're doing this:

t * (p1 + AB)

instead of

p1 + AB * t

Also, the lines where you calculate ab2 and ab_ap can be reduced to the dot product of two vectors (ab dot ab for ab2, and ab dot ap for ab_ap).


THANKS MAN You solved the problem!

AB = AB *t;
D3DXVec3Add(&Closest,&p1,&AB);

Share this post


Link to post
Share on other sites
Quote:
Original post by Meshboy
Quote:
Original post by Evil Steve
Your C# code should work as C++ with the single change of replacing "Vector3" with "D3DXVECTOR3".


No, you can´t use - and + when using Vectors in C++.
No, you can do it in C++, but not plain C. This code compiles and runs fine for me:

#include <d3dx9.h>

int main()
{
D3DXVECTOR3 a(0, 1, 2);
D3DXVECTOR3 b(3, 4, 5);
D3DXVECTOR3 c = a - b;
return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!