Changeset 10031 for code/branches/turretFS14/src/modules/objects/Turret.cc
- Timestamp:
- Apr 10, 2014, 4:06:03 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/turretFS14/src/modules/objects/Turret.cc
r10021 r10031 44 44 { 45 45 RegisterObject(Turret); 46 this->startOrientInv_ = Quaternion::IDENTITY; 46 this->startOrient_ = Quaternion::IDENTITY; 47 this->startDir_ = Vector3::ZERO; 48 this->localZ_ = Vector3::UNIT_Z; 49 this->localY_ = Vector3::UNIT_Y; 50 this->localX_ = Vector3::UNIT_X; 47 51 this->maxPitch_ = 0; 48 52 this->maxYaw_ = 0; 53 this->attackRadius_ = 200; 49 54 this->gotOrient_ = false; 50 55 this->rotationThrust_ = 50; … … 63 68 64 69 void Turret::rotatePitch(const Vector2& value) 65 { 66 if (this->maxPitch_ == 0) 67 { 68 return; 69 } 70 if (this->maxPitch_ >= 180) //no need to check, if the limit too big 71 { 72 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); 73 return; 74 } 75 76 Quaternion drot = startOrientInv_ * this->getOrientation(); 77 78 Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180); 79 Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); 80 Ogre::Real lowerBound = offset - this->maxPitch_; 81 Ogre::Real upperBound = offset + this->maxPitch_; 82 if (lowerBound < -180) //Avoid wrapping around of the boundaries 83 { 84 lowerBound += this->maxPitch_; 85 upperBound += this->maxPitch_; 86 val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here 87 } 88 else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) 89 { 90 lowerBound -= this->maxPitch_; 91 upperBound -= this->maxPitch_; 92 val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here 93 } 94 if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 70 { 71 //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) 72 /* 73 Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); 74 Vector3 currentDirProjected = currentDir; 75 currentDirProjected.x = 0; 76 Vector3 startDirProjected = this->startDir_; 77 startDirProjected.x = 0; 78 Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); 79 //orxout() << "Pitch: " << angle << endl; 80 //if(angle < this->maxPitch_ || (currentDirProjected.y <= 0 && value.x > 0) || (currentDirProjected.y > 0 && value.x < 0) ) 95 81 { 96 82 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); 97 83 } 98 return; 84 */ 85 this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f); 99 86 } 100 87 101 88 void Turret::rotateYaw(const Vector2& value) 102 89 { 103 if (this->maxPitch_ == 0) 104 { 105 return; 106 } 107 if (this->maxPitch_ >= 180) //no need to check, if the limit too big 90 //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?) 91 /* 92 Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); 93 Vector3 currentDirProjected = currentDir; 94 currentDirProjected.y = 0; 95 Vector3 startDirProjected = this->startDir_; 96 startDirProjected.y = 0; 97 Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees(); 98 orxout() << "Yaw: " << angle << endl; 99 if(angle < this->maxYaw_ || (currentDirProjected.x <= 0 && value.x < 0) || (currentDirProjected.x > 0 && value.x > 0)) 108 100 { 109 101 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); 110 return;111 102 } 112 113 Quaternion drot = startOrientInv_ * this->getOrientation(); 114 115 Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180); 116 Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); 117 Ogre::Real lowerBound = offset - this->maxPitch_; 118 Ogre::Real upperBound = offset + this->maxPitch_; 119 if (lowerBound < -180) //Avoid wrapping around of the boundaries 120 { 121 lowerBound += this->maxPitch_; 122 upperBound += this->maxPitch_; 123 val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here 124 } 125 else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) 126 { 127 lowerBound -= this->maxPitch_; 128 upperBound -= this->maxPitch_; 129 val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here 130 } 131 if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 132 { 133 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); 134 } 135 return; 103 */ 104 this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f); 136 105 } 137 106 138 107 void Turret::rotateRoll(const Vector2& value) 139 108 { 140 return; //Standard turrets don't roll 109 this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f); 110 } 111 112 bool Turret::isInRange(Vector3 position) 113 { 114 Vector3 distance = position - this->getPosition(); 115 if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_)) 116 { 117 return false; 118 } 119 120 Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_); 121 Vector3 dirProjected = dir; 122 dirProjected.x = 0; 123 Vector3 startDirProjected = this->startDir_; 124 startDirProjected.x = 0; 125 Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); 126 if(angle > this->maxPitch_) 127 { 128 return false; 129 } 130 131 dirProjected = dir; 132 dirProjected.y = 0; 133 startDirProjected = this->startDir_; 134 startDirProjected.y = 0; 135 angle = startDirProjected.angleBetween(dirProjected).valueDegrees(); 136 if(angle > this->maxYaw_) 137 { 138 return false; 139 } 140 return true; 141 141 } 142 142 … … 145 145 XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode); 146 146 XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode); 147 XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode); 147 148 SUPER(Turret, XMLPort, xmlelement, mode); 148 149 } … … 154 155 if(!gotOrient_) 155 156 { 156 startOrientInv_ = this->getOrientation().Inverse(); 157 gotOrient_ = true; 157 this->startOrient_ = this->getOrientation(); 158 this->localX_ = this->startOrient_ * this->localX_; 159 this->localX_.normalise(); 160 this->localY_ = this->startOrient_ * this->localY_; 161 this->localY_.normalise(); 162 this->localZ_ = this->startOrient_ * this->localZ_; 163 this->localZ_.normalise(); 164 165 //startDir should always be (0,0,-1) 166 this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_); 167 168 this->gotOrient_ = true; 158 169 } 159 Quaternion drot = startOrientInv_ * this->getOrientation(); 160 orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl; 161 170 162 171 this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_; 163 172 this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_); … … 165 174 } 166 175 167 168 Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)169 {170 if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }171 val -= lowerBound; //adjust to 0172 Ogre::Real rangeSize = upperBound - lowerBound;173 if (rangeSize == 0){ return upperBound; } //avoid dividing by 0174 return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;175 }176 177 176 }
Note: See TracChangeset
for help on using the changeset viewer.