#include #include #include #include #include "bullet.h" /************************************************************************/ /* Performer type data */ pfType *bullet::classType_ = NULL; void bullet::init(void) { if (classType_ == NULL) { pfDCS::init(); /* Initialize the parent class */ classType_ = new pfType(pfDCS::getClassType(), "bullet"); /* Create the new pfType for this class, based on the parent class's type */ } } /************************************************************************/ bullet::bullet(void) { pfGeoSet *ball; pfGeode *geode; setType(classType_); /* set the Performer type of this instance */ active_ = 1; world_ = NULL; CAVEGetPosition(CAVE_WAND_NAV,position_.vec); setTrans(position_[0],position_[1],position_[2]); CAVEGetVector(CAVE_WAND_FRONT_NAV,velocity_.vec); velocity_ *= 30.0f; gravity_ = 32.0f; prevTime_ = pfGetTime(); ball = pfdNewSphere(24,pfGetSharedArena()); geode = new pfGeode; geode->addGSet(ball); addChild(geode); setTravMask(PFTRAV_ISECT, 0, PFTRAV_SELF|PFTRAV_DESCEND, PF_SET); } void bullet::setWorld(pfNode *world) { world_ = world; } void bullet::setGravity(float g) { gravity_ = g; } int bullet::app(pfTraverser *trav) { float dt; if (!active_) return pfDCS::app(trav); dt = pfGetTime() - prevTime_; if (dt < 1.0f) { pfVec3 prevPos = position_; position_ += velocity_ * dt; if ((position_[2] < 0.0f) || (hitObject(prevPos))) { active_ = 0; setTravMask(PFTRAV_ISECT, 0xffffffff, PFTRAV_SELF|PFTRAV_DESCEND|PFTRAV_IS_CACHE, PF_SET); } else velocity_[2] -= gravity_ * dt; } setTrans(position_[0],position_[1],position_[2]); prevTime_ = pfGetTime(); return pfDCS::app(trav); } int bullet::needsApp(void) { return TRUE; } int bullet::hitObject(pfVec3 prevPos) { pfSegSet segset; pfHit **hits[32]; segset.activeMask = 1; segset.isectMask = 0xFFFF; segset.discFunc = NULL; segset.bound = NULL; segset.mode = PFTRAV_IS_PRIM; segset.segs[0].makePts(prevPos,position_); if (world_->isect(&segset, hits)) { pfVec3 isectPoint; pfMatrix xmat; (*hits[0])->query(PFQHIT_POINT, isectPoint.vec); (*hits[0])->query(PFQHIT_XFORM, (float*)xmat.mat); position_.xformPt(isectPoint, xmat); return 1; } else return 0; }