C++ guru, please, could you help me to translate this small piece of C++ OOP to PowerBASIC OOP.
Especially the class particle, and the class emitter using particle.
class rsVec{
public:
float v[3];
rsVec();
rsVec(float xx, float yy, float zz);
virtual ~rsVec();
void set(float xx, float yy, float zz);
float length();
float normalize();
float dot(rsVec);
void cross(rsVec, rsVec);
void scale(float);
void transPoint(const rsMatrix &m);
void transVec(const rsMatrix &m);
int almostEqual(rsVec vec, float tolerance);
float & operator [] (int i) {return v[i];}
const float & operator [] (int i) const {return v[i];}
rsVec & operator = (const rsVec &vec)
{v[0]=vec[0];v[1]=vec[1];v[2]=vec[2];return *this;}
rsVec operator + (const rsVec &vec)
{return(rsVec(v[0]+vec[0], v[1]+vec[1], v[2]+vec[2]));}
rsVec operator - (const rsVec &vec)
{return(rsVec(v[0]-vec[0], v[1]-vec[1], v[2]-vec[2]));}
rsVec operator * (const float &mul)
{return(rsVec(v[0]*mul, v[1]*mul, v[2]*mul));}
rsVec operator / (const float &div)
{float rec = 1.0f/div; return(rsVec(v[0]*rec, v[1]*rec, v[2]*rec));}
rsVec & operator += (const rsVec &vec)
{v[0]+=vec[0];v[1]+=vec[1];v[2]+=vec[2];return *this;}
rsVec & operator -= (const rsVec &vec)
{v[0]-=vec[0];v[1]-=vec[1];v[2]-=vec[2];return *this;}
rsVec & operator *= (const rsVec &vec)
{v[0]*=vec[0];v[1]*=vec[1];v[2]*=vec[2];return *this;}
rsVec & operator *= (const float &mul)
{v[0]*=mul;v[1]*=mul;v[2]*=mul;return *this;}
};
class particle{
public:
rsVec pos;
rsVec rgb;
float size;
};
class emitter:public particle{
public:
rsVec oldpos;
rsVec targetpos;
emitter();
~emitter(){};
void settargetpos(rsVec target){oldpos = pos; targetpos = target;};
void interppos(float n){pos = oldpos * (1.0f - n) + targetpos * n;};
void update(){};
};
emitter::emitter(){
pos = rsVec(rsRandf(1000.0f) - 500.0f, rsRandf(1000.0f) - 500.0f, rsRandf(1000.0f) - 500.0f);
}
Patrice,
I am NOT a c++ guru but there are things that cannot be translated from c++ classes to PB COM classes. One of these are the initializations done in c++ constructors.
James
Here is what i came with PB, but the C++ syntax is realy more powerful ...
CLASS CrsVec
INSTANCE v() AS SINGLE
' -- Object initialization
CLASS METHOD CREATE()
DIM v(2)
v(0) = 0.0!
v(1) = 0.0!
v(2) = 0.0!
END METHOD
' -- Object resources deletion
CLASS METHOD DESTROY()
ERASE V()
END METHOD
INTERFACE rsVec
INHERIT IUNKNOWN
METHOD set(BYVAL x AS SINGLE, BYVAL y AS SINGLE, BYVAL z AS SINGLE)
v(0) = x
v(1) = y
v(2) = z
END METHOD
METHOD get() AS DWORD
METHOD = VARPTR(v(0))
END METHOD
END INTERFACE
END CLASS
CLASS Cemitter
INSTANCE pPos AS rsVec
INSTANCE pRgb AS rsVec
INSTANCE pSize AS SINGLE
INSTANCE oldpos AS rsVec
INSTANCE targetpos AS rsVec
' -- Object initialization
CLASS METHOD CREATE()
END METHOD
' -- Object resources deletion
CLASS METHOD DESTROY()
END METHOD
INTERFACE emitter
INHERIT IUNKNOWN
METHOD settargetpos(target AS rsVec)
oldpos = pPos
targetpos = target
END METHOD
METHOD interppos(n AS SINGLE)
'pPos = oldpos * (1.0! - n) + targetpos * n
DIM op(2) AS SINGLE AT oldpos.get()
DIM tp(2) AS SINGLE AT targetpos.get()
DIM p(2) AS SINGLE
LOCAL K AS LONG
FOR K = 0 TO 3: p(K) = op(K) * (1.0! - n) + tp(K) * n: NEXT
pPos.set(p(0), p(1), p(2))
END METHOD
METHOD update()
END METHOD
END INTERFACE
END CLASS