[1378] | 1 | #include "dxstdafx.h"
|
---|
| 2 | #include ".\particlecube.h"
|
---|
| 3 |
|
---|
| 4 | ParticleCube::ParticleCube(void) : Node()
|
---|
| 5 | {
|
---|
| 6 | this->width = 0;
|
---|
| 7 | this->height = 0;
|
---|
| 8 | this->depth = 0;
|
---|
| 9 | this->calcLocalPosition = false;
|
---|
| 10 | this->calcLocalVelocity = false;
|
---|
| 11 | this->updatePlanes();
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | ParticleCube::~ParticleCube(void)
|
---|
| 15 | {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | void ParticleCube::setDimensions(float _w, float _h, float _d)
|
---|
| 19 | {
|
---|
| 20 | this->width = _w;
|
---|
| 21 | this->height = _h;
|
---|
| 22 | this->depth = _d;
|
---|
| 23 | this->updatePlanes();
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | void ParticleCube::update(float dt)
|
---|
| 27 | {
|
---|
| 28 | this->myParticleList.clear();
|
---|
| 29 | this->myParticlePositionList.clear();
|
---|
| 30 | this->myParticleVelocityList.clear();
|
---|
| 31 | this->updatePlanes();
|
---|
| 32 | std::list<SPTR<Node> >* allParticles = this->myScene->getParticleList();
|
---|
| 33 | std::list<SPTR<Node> >::iterator it;
|
---|
| 34 | Node *node;
|
---|
| 35 | bool inside;
|
---|
| 36 | Vector nPos;
|
---|
| 37 | for(it=allParticles->begin();it!=allParticles->end();it++) {
|
---|
| 38 | node = (*it).get();
|
---|
| 39 | inside = true;
|
---|
| 40 | for(unsigned int j=0;j<6;j++) {
|
---|
| 41 | nPos = node->getAbsolutePosition();
|
---|
| 42 | if(wall[j].inFront(nPos)) {
|
---|
| 43 | inside = false;
|
---|
| 44 | break;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | if(inside) {
|
---|
| 48 | D3DXMATRIXA16 invWorldMat;
|
---|
| 49 | Vector tempPos;
|
---|
| 50 | Vector tempVel;
|
---|
| 51 | if(this->calcLocalPosition || this->calcLocalVelocity) {
|
---|
| 52 | D3DXMatrixInverse(&invWorldMat, NULL, &this->myWorldMatrix);
|
---|
| 53 | }
|
---|
| 54 | if(this->calcLocalPosition) {
|
---|
| 55 | D3DXVec4Transform(&tempPos, &nPos, &invWorldMat);
|
---|
| 56 | this->myParticlePositionList.push_back(tempPos);
|
---|
| 57 | }
|
---|
| 58 | if(this->calcLocalVelocity) {
|
---|
| 59 | tempVel = Vector(node->getActor()->getLinearVelocity());
|
---|
| 60 | D3DXVec4Transform(&tempVel, &tempVel, &invWorldMat);
|
---|
| 61 | tempVel = tempVel - nPos;
|
---|
| 62 | this->myParticleVelocityList.push_back(tempVel);
|
---|
| 63 | }
|
---|
| 64 | this->myParticleList.push_back(node);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void ParticleCube::addForceToParticle(Node* particle, Vector force)
|
---|
| 70 | {
|
---|
| 71 | NxActor* actor = particle->getActor();
|
---|
| 72 | if(actor!=NULL) {
|
---|
| 73 | //force = this->getAbsoluteVector(force) - this->getAbsolutePosition();
|
---|
| 74 | this->getAbsoluteDirectionVector(force, force);
|
---|
| 75 | actor->addForceAtLocalPos(force.getNxVector(), NxVec3(0,0,0));
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | void ParticleCube::addVelocityToParticle(Node* particle, Vector velocity)
|
---|
| 80 | {
|
---|
| 81 | NxActor* actor = particle->getActor();
|
---|
| 82 | if(actor!=NULL) {
|
---|
| 83 | //velocity = this->getAbsoluteVector(velocity) - this->getAbsolutePosition();
|
---|
| 84 | this->getAbsoluteDirectionVector(velocity, velocity);
|
---|
| 85 | Vector tempV(actor->getLinearVelocity());
|
---|
| 86 | velocity = velocity + tempV;
|
---|
| 87 | actor->setLinearVelocity(velocity.getNxVector());
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void ParticleCube::updatePlanes()
|
---|
| 92 | {
|
---|
| 93 | Vector p[6];
|
---|
| 94 | Vector np[6];
|
---|
| 95 |
|
---|
| 96 | p[FRONT].setXYZ(0, 0, this->depth/2);
|
---|
| 97 | p[BACK].setXYZ(0, 0, -this->depth/2);
|
---|
| 98 | p[TOP].setXYZ(0, this->height/2, 0);
|
---|
| 99 | p[BOTTOM].setXYZ(0, -this->height/2, 0);
|
---|
| 100 | p[LEFT].setXYZ(this->width/2, 0, 0);
|
---|
| 101 | p[RIGHT].setXYZ(-this->width/2, 0, 0);
|
---|
| 102 |
|
---|
| 103 | np[FRONT].setXYZ(0,0,1);
|
---|
| 104 | np[BACK].setXYZ(0,0,-1);
|
---|
| 105 | np[TOP].setXYZ(0,1,0);
|
---|
| 106 | np[BOTTOM].setXYZ(0,-1,0);
|
---|
| 107 | np[LEFT].setXYZ(1, 0, 0);
|
---|
| 108 | np[RIGHT].setXYZ(-1, 0, 0);
|
---|
| 109 |
|
---|
| 110 | for(int i=0;i<6;i++) {
|
---|
| 111 | this->getAbsoluteVector(p[i], p[i]);
|
---|
| 112 | this->getAbsoluteVector(np[i], np[i]);
|
---|
| 113 | np[i] = np[i] - p[i];
|
---|
| 114 | wall[i].setPoint(p[i]);
|
---|
| 115 | wall[i].setNormal(np[i]);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|