ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

turn.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/movement/turn.h"
00024 
00025 #include "math/matrix3.h"
00026 
00027 #include "engines/stark/resources/anim.h"
00028 #include "engines/stark/resources/item.h"
00029 
00030 #include "engines/stark/services/global.h"
00031 #include "engines/stark/services/services.h"
00032 #include "engines/stark/services/stateprovider.h"
00033 
00034 namespace Stark {
00035 
00036 Turn::Turn(Resources::FloorPositionedItem *item) :
00037         Movement(item),
00038         _item3D(item),
00039         _turnSpeed(_defaultTurnAngleSpeed) {
00040 }
00041 
00042 Turn::~Turn() {
00043 }
00044 
00045 void Turn::onGameLoop() {
00046     // Compute the direction to turn towards
00047     Math::Vector3d direction = _targetDirection;
00048     direction.z() = 0;
00049     direction.normalize();
00050 
00051     // Compute the angle with the current character direction
00052     Math::Vector3d currentDirection = _item3D->getDirectionVector();
00053     float directionDeltaAngle = computeAngleBetweenVectorsXYPlane(currentDirection, direction);
00054 
00055     // If the angle between the current direction and the new one is too high,
00056     // make the character turn on itself until the angle is low enough
00057     TurnDirection turnDirection;
00058     if (ABS(directionDeltaAngle) > getAngularSpeed() + 0.1f) {
00059         turnDirection = directionDeltaAngle < 0 ? kTurnLeft : kTurnRight;
00060     } else {
00061         turnDirection = kTurnNone;
00062     }
00063 
00064     if (turnDirection == kTurnNone) {
00065         direction = _targetDirection;
00066     } else {
00067         // Make the character turn towards the target direction
00068         direction = currentDirection;
00069 
00070         Math::Matrix3 rot;
00071         rot.buildAroundZ(turnDirection == kTurnLeft ? -getAngularSpeed() : getAngularSpeed());
00072         rot.transformVector(&direction);
00073     }
00074 
00075     // Update the item's direction
00076     _item3D->setDirection(computeAngleBetweenVectorsXYPlane(direction, Math::Vector3d(1.0, 0.0, 0.0)));
00077 
00078     // Check if we are close enough to the destination to stop
00079     if (direction == _targetDirection) {
00080         stop();
00081     }
00082 }
00083 
00084 float Turn::getAngularSpeed() const {
00085     return _turnSpeed * StarkGlobal->getMillisecondsPerGameloop();
00086 }
00087 
00088 void Turn::setTargetDirection(const Math::Vector3d &direction) {
00089     _targetDirection = direction;
00090 }
00091 
00092 void Turn::setSpeed(float speed) {
00093     _turnSpeed = speed;
00094 }
00095 
00096 uint32 Turn::getType() const {
00097     return kTypeTurn;
00098 }
00099 
00100 void Turn::saveLoad(ResourceSerializer *serializer) {
00101     serializer->syncAsVector3d(_targetDirection);
00102     serializer->syncAsFloat(_turnSpeed);
00103 }
00104 
00105 } // End of namespace Stark


Generated on Sat Mar 16 2019 05:01:57 for ResidualVM by doxygen 1.7.1
curved edge   curved edge