# Help with C++ From C#

## Recommended Posts

Meshboy    130
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

##### Share on other sites
Legendweaver    197
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

##### Share on other sites
Evil Steve    2017
Your C# code should work as C++ with the single change of replacing "Vector3" with "D3DXVECTOR3".

#### Share this post

##### Share on other sites
Ezbez    1164
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

##### Share on other sites
Meshboy    130
Quote:
 Original post by Evil SteveYour 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

##### Share on other sites
Meshboy    130
Quote:
 Original post by EzbezWell, in the C++ version, you're doing this:t * (p1 + AB)instead ofp1 + AB * tAlso, 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

##### Share on other sites
Evil Steve    2017
Quote:
Original post by Meshboy
Quote:
 Original post by Evil SteveYour 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;}

## 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