Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "BezierProjectile.h"
#include "T3D/shapeBase.h"
#include "core/stream/bitStream.h"
#include "sim/netConnection.h"
IMPLEMENT_CO_DATABLOCK_V1(BezierProjectileData);
IMPLEMENT_CO_NETOBJECT_V1(BezierProjectile);
//--------------------------------------------
// BezierProjectileData
//--------------------------------------------
//--------------------------------------------
// Constructor
//--------------------------------------------
BezierProjectileData::BezierProjectileData()
{
bezier = Point3F(0);
}
//--------------------------------------------
// initPersistFields
//--------------------------------------------
void BezierProjectileData::initPersistFields()
{
addGroup("BezierProjectileData");
addField("bezierweights", TypePoint3F, Offset(bezier, BezierProjectileData), "");
endGroup("BezierProjectileData");
Parent::initPersistFields();
}
//--------------------------------------------
// packData
//--------------------------------------------
void BezierProjectileData::packData(BitStream* stream)
{
Parent::packData(stream);
stream->writeCompressedPoint(bezier);
}
//--------------------------------------------
// unpackData
//--------------------------------------------
void BezierProjectileData::unpackData(BitStream* stream)
{
Parent::unpackData(stream);
stream->readCompressedPoint(&bezier);
}
//--------------------------------------------
// BezierProjectile
//--------------------------------------------
//--------------------------------------------
// Constructor
//--------------------------------------------
BezierProjectile::BezierProjectile()
{
TargetPosition = Point3F(0);
BezierWeights = Point3F::Max;
Parent::FXProjectile();
}
//--------------------------------------------
// initPersistFields
//--------------------------------------------
void BezierProjectile::initPersistFields()
{
addField("TargetPosition", TypePoint3F, Offset(TargetPosition, BezierProjectile), "");
addField("BezierWeights", TypePoint3F, Offset(BezierWeights, BezierProjectile), "");
Parent::initPersistFields();
}
bool BezierProjectile::onNewDataBlock( GameBaseData *dptr, bool reload )
{
mDataBlock = dynamic_cast< BezierProjectileData* >( dptr );
if( !mDataBlock || !Parent::onNewDataBlock( dptr, reload ) )
return false;
if(BezierWeights == Point3F::Max)
BezierWeights = ((BezierProjectileData*)mDataBlock)->bezier;
scriptOnNewDataBlock();
return true;
}
//--------------------------------------------
// packUpdate
//--------------------------------------------
U32 BezierProjectile::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
{
U32 retMask = Parent::packUpdate(conn, mask, stream);
if(stream->writeFlag(mask & InitialUpdateMask))
stream->writeCompressedPoint(TargetPosition);
stream->writeCompressedPoint(BezierWeights);
return retMask;
}
//--------------------------------------------
// unpackUpdate
//--------------------------------------------
void BezierProjectile::unpackUpdate(NetConnection* conn, BitStream* stream)
{
Parent::unpackUpdate(conn, stream);
if(stream->readFlag())
stream->readCompressedPoint(&TargetPosition);
stream->readCompressedPoint(&BezierWeights);
}
//--------------------------------------------
// updatePosition
//--------------------------------------------
void BezierProjectile::updatePosition(F32 dt, Point3F &oldPosition, Point3F &newPosition)
{
oldPosition = mCurrPosition;
if ( mDataBlock->isBallistic )
mCurrVelocity.z -= 9.81 * mDataBlock->gravityMod * dt;
Point3F end;
if(mHoming && mTargetObject)
end = mTargetObject->getWorldBox().getCenter();
else
end = TargetPosition;
F32 t = (F32)mCurrTick / (F32)mDataBlock->lifetime;
MatrixF mat = mInitialTransform.getMatrix();
Point3F halfWay = mInitialPosition+((end - mInitialPosition) / 2);
Point3F bezier;
mat.mulV(BezierWeights, &bezier);
halfWay += bezier;
Point3F p0, p1, p2;
p0 = ((1-t)*(1-t))*mInitialPosition;
p1 = 2*(1-t)*t*halfWay;
p2 = (t*t*end);
newPosition = p0+p1+p2;
mCurrVelocity = (newPosition - oldPosition)/dt;
newPosition = oldPosition + mCurrVelocity * dt;
}
#ifndef BEZIER_PROJECTILE_H
#define BEZIER_PROJECTILE_H
#include "FXProjectile.h"
class BezierProjectileData : public FXProjectileData
{
typedef FXProjectileData Parent;
public:
//------- Enums -------
public:
//--------------------------------------------
// SimDataBlock
//--------------------------------------------
virtual void packData(BitStream*);
virtual void unpackData(BitStream*);
static void initPersistFields();
//--------------------------------------------
// SpellProjectileData
//--------------------------------------------
BezierProjectileData();
//------- Fields -------
Point3F bezier;
DECLARE_CONOBJECT(BezierProjectileData);
};
class BezierProjectile : public FXProjectile
{
typedef FXProjectile Parent;
public:
//--------------------------------------------
// SimObject
//--------------------------------------------
static void initPersistFields();
//--------------------------------------------
// GameBase
//--------------------------------------------
virtual U32 packUpdate(NetConnection* conn, U32 mask, BitStream* stream);
virtual void unpackUpdate(NetConnection* conn, BitStream* stream);
virtual bool onNewDataBlock(GameBaseData* dptr, bool reload);
//--------------------------------------------
// SpellProjectile
//--------------------------------------------
BezierProjectile();
virtual void updatePosition(F32 dt, Point3F &oldPosition, Point3F &newPosition);
//------- Fields -------
Point3F TargetPosition;
Point3F BezierWeights;
void setWeights(Point3F weights) { BezierWeights = weights; };
void setTargetPosition(Point3F pos) { TargetPosition = pos; };
DECLARE_CONOBJECT(BezierProjectile);
};
#endif // BEZIER_PROJECTILE_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment