PDA

View Full Version : converting c code



tux
24-05-2005, 03:45 PM
im trying to convert a c demo and im stuck. heres the code (just 1 line)


nozzleDampForce = - (nozzleVeloc % nozzleDir) * mass * nozzleDampCoef;


ive never seen a % in code before so i dont know what to do with it.

this is how the delphi code is looking so far

LNozzleDampForce := -{(LNozzleVelocity % LNozzleDirection) *} Hovercraft.Mass * LNozzleDampCoef;

LNozzleVelocity, LNozzleDirection are Vectors (array[0..2] of Single) and Hovercraft.Mass, LNozzleDampForce and LNozzleDampCoef are singles.

any help is great as im pulling my hair out over this :D

WiZz
24-05-2005, 04:13 PM
it's the same like mod :)

tux
24-05-2005, 04:15 PM
hmm, mod a vector? sure i can do that but then what to do with the vector? use the vector length?

[edit]

LTempVector[0] := LNozzleVelocity[0] mod LNozzleDirection[0];
LTempVector[1] := LNozzleVelocity[1] mod LNozzleDirection[1];
LTempVector[2] := LNozzleVelocity[2] mod LNozzleDirection[2];

doesnt work anyway :?

Paulius
24-05-2005, 04:37 PM
I guess it's an overridden operator, post the class of nozzleDir

tux
24-05-2005, 04:48 PM
its a vector, an array of 3 singles

[edit] im so dumb :D heres the c file, from reading it. its just a dot product?


//************************************************** ******************
// Newton Game dynamics
// copyright 2000-2004
// By Julio Jerez
// VC: 6.0
// simple 4d vector class
//************************************************** ******************

#include <stdafx.h>
#ifndef __dVector__
#define __dVector__

#include <math.h>

// small but very effective 4 dimetional template vector class

template<class T>
class TemplateVector
&#123;
public&#58;
TemplateVector &#40;&#41;;
TemplateVector &#40;const T *ptr&#41;;
TemplateVector &#40;T m_x, T m_y, T m_z, T m_w&#41;;
TemplateVector Scale &#40;T s&#41; const;

T& operator&#91;&#93; &#40;int i&#41;;
const T& operator&#91;&#93; &#40;int i&#41; const;

TemplateVector operator+ &#40;const TemplateVector &A&#41; const;
TemplateVector operator- &#40;const TemplateVector &A&#41; const;
TemplateVector &operator+= &#40;const TemplateVector &A&#41;;
TemplateVector &operator-= &#40;const TemplateVector &A&#41;;

// return dot product
T operator% &#40;const TemplateVector &A&#41; const;

// return cross product
TemplateVector operator* &#40;const TemplateVector &B&#41; const;

// component wiise multiplacation
TemplateVector CompProduct &#40;const TemplateVector &A&#41; const;

T m_x;
T m_y;
T m_z;
T m_w;
&#125;;


class dVector&#58; public TemplateVector<dFloat32>
&#123;
public&#58;
dVector&#40;&#41;;
dVector &#40;const TemplateVector<dFloat32>& v&#41;;
dVector &#40;const dFloat32 *ptr&#41;;
dVector &#40;dFloat32 x, dFloat32 y, dFloat32 z, dFloat32 w = 1.0&#41;;
&#125;;





template<class T>
TemplateVector<T>&#58;&#58;TemplateVector&#40;&#41; &#123;&#125;

template<class T>
TemplateVector<T>&#58;&#58;TemplateVector&#40;const T *ptr&#41;
&#123;
m_x = ptr&#91;0&#93;;
m_y = ptr&#91;1&#93;;
m_z = ptr&#91;2&#93;;
m_w = 1.0;
&#125;

template<class T>
TemplateVector<T>&#58;&#58;TemplateVector&#40;T x, T y, T z, T w&#41;
&#123;
m_x = x;
m_y = y;
m_z = z;
m_w = w;
&#125;


template<class T>
typename T& TemplateVector<T>&#58;&#58;operator&#91;&#93; &#40;int i&#41;
&#123;
return &#40;&m_x&#41;&#91;i&#93;;
&#125;

template<class T>
typename const T& TemplateVector<T>&#58;&#58;operator&#91;&#93; &#40;int i&#41; const
&#123;
return &#40;&m_x&#41;&#91;i&#93;;
&#125;

template<class T>
typename TemplateVector<T> TemplateVector<T>&#58;&#58;Scale &#40;T scale&#41; const
&#123;
return TemplateVector<T> &#40;m_x * scale, m_y * scale, m_z * scale, m_w&#41;;
&#125;


template<class T>
typename TemplateVector<T> TemplateVector<T>&#58;&#58;operator+ &#40;const TemplateVector<T> &B&#41; const
&#123;
return TemplateVector<T> &#40;m_x + B.m_x, m_y + B.m_y, m_z + B.m_z, m_w&#41;;
&#125;

template<class T>
typename TemplateVector<T>& TemplateVector<T>&#58;&#58;operator+= &#40;const TemplateVector<T> &A&#41;
&#123;
m_x += A.m_x;
m_y += A.m_y;
m_z += A.m_z;
return *this;
&#125;

template<class T>
typename TemplateVector<T> TemplateVector<T>&#58;&#58;operator- &#40;const TemplateVector<T> &A&#41; const
&#123;
return TemplateVector<T> &#40;m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w&#41;;
&#125;

template<class T>
typename TemplateVector<T>& TemplateVector<T>&#58;&#58;operator-= &#40;const TemplateVector<T> &A&#41;
&#123;
m_x -= A.m_x;
m_y -= A.m_y;
m_z -= A.m_z;
return *this;
&#125;


template<class T>
typename T TemplateVector<T>&#58;&#58;operator% &#40;const TemplateVector<T> &A&#41; const
&#123;
return m_x * A.m_x + m_y * A.m_y + m_z * A.m_z;
&#125;


template<class T>
typename TemplateVector<T> TemplateVector<T>&#58;&#58;operator* &#40;const TemplateVector<T> &B&#41; const
&#123;
return TemplateVector<T> &#40;m_y * B.m_z - m_z * B.m_y,
m_z * B.m_x - m_x * B.m_z,
m_x * B.m_y - m_y * B.m_x, m_w&#41;;
&#125;



template<class T>
typename TemplateVector<T> TemplateVector<T>&#58;&#58;CompProduct &#40;const TemplateVector<T> &A&#41; const
&#123;
return TemplateVector<T> &#40;m_x * A.m_x, m_y * A.m_y, m_z * A.m_z, A.m_w&#41;;
&#125;



inline dVector&#58;&#58;dVector&#40;&#41;
&#58;TemplateVector<dFloat32>&#40;&#41;
&#123;
&#125;

inline dVector&#58;&#58;dVector &#40;const TemplateVector<dFloat32>& v&#41;
&#58;TemplateVector<dFloat32>&#40;v&#41;
&#123;
&#125;

inline dVector&#58;&#58;dVector &#40;const dFloat32 *ptr&#41;
&#58;TemplateVector<dFloat32>&#40;ptr&#41;
&#123;
&#125;

inline dVector&#58;&#58;dVector &#40;dFloat32 x, dFloat32 y, dFloat32 z, dFloat32 w&#41;
&#58;TemplateVector<dFloat32>&#40;x, y, z, w&#41;
&#123;
&#125;

#endif

Paulius
24-05-2005, 04:52 PM
template<class T>
typename T TemplateVector<T>::operator% (const TemplateVector<T> &A) const
{
return m_x * A.m_x + m_y * A.m_y + m_z * A.m_z;
}

dotproduct

tux
24-05-2005, 05:50 PM
thanks. but its not giving the result i need :(

Paulius
24-05-2005, 06:17 PM
It's not very clear what kind of result you need and what you get

cragwolf
24-05-2005, 06:32 PM
I think that "LNozzleVelocity % LNozzleDirection" would be equivalent to:

LNozzleVelocity[0] * LNozzleDirection[0] + LNozzleVelocity[1] * LNozzleDirection[1] + LNozzleVelocity[2] * LNozzleDirection[2]

tux
24-05-2005, 07:24 PM
It's not very clear what kind of result you need and what you get


well its for a hovercraft, it sits on springs, i just cant get it to damp properly. ill figure it out