Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox/controllers/NewHumanController.cc @ 6058

Last change on this file since 6058 was 6058, checked in by rgrieder, 14 years ago

Found the firing direction bug: "Fixed goes first" we were told a few weeks ago in a lecture about spatial coordinates.
Here the problem was a quaternion multiplication.

File size: 7.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Michael Wirth
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "NewHumanController.h"
30
31#include <OgreRay.h>
32#include <OgreSceneQuery.h>
33#include <OgreCamera.h>
34#include <OgreSceneManager.h>
35
36#include "core/CoreIncludes.h"
37#include "core/ConsoleCommand.h"
38#include "worldentities/ControllableEntity.h"
39#include "infos/PlayerInfo.h"
40#include "overlays/OrxonoxOverlay.h"
41#include "graphics/Camera.h"
42#include "sound/SoundManager.h"
43#include "Scene.h"
44
45namespace orxonox
46{
47    CreateUnloadableFactory(NewHumanController);
48
49    NewHumanController::NewHumanController(BaseObject* creator) : HumanController(creator)
50    {
51        RegisterObject(NewHumanController);
52
53        overlaySize_ = 0.08;
54        controlMode_ = 0;
55
56        crossHairOverlay_ = new OrxonoxOverlay(this);
57        crossHairOverlay_->setBackgroundMaterial("Orxonox/Crosshair3");
58        crossHairOverlay_->setSize(Vector2(overlaySize_, overlaySize_));
59        crossHairOverlay_->show();
60
61        // HACK: Define which objects are targetable when considering the creator of an orxonox::Model
62        this->targetMask_.exclude(ClassByString("BaseObject"));
63        this->targetMask_.include(ClassByString("WorldEntity"));
64        this->targetMask_.exclude(ClassByString("Projectile"));
65    }
66
67    NewHumanController::~NewHumanController()
68    {
69        if (this->isInitialized())
70        {
71        }
72    }
73
74    void NewHumanController::tick(float dt)
75    {
76        crossHairOverlay_->setPosition(Vector2(static_cast<float>(this->currentYaw_)/2*-1+.5-overlaySize_/2, static_cast<float>(this->currentPitch_)/2*-1+.5-overlaySize_/2));
77
78        HumanController::tick(dt);
79    }
80
81    /*void NewHumanController::tick(float dt)
82    {
83        if (GameMode::playsSound() && NewHumanController::localController_s && NewHumanController::localController_s->controllableEntity_)
84        {
85            // Update sound listener
86            Camera* camera = NewHumanController::localController_s->controllableEntity_->getCamera();
87            if (camera)
88            {
89                SoundManager::getInstance().setListenerPosition(camera->getWorldPosition());
90                SoundManager::getInstance().setListenerOrientation(camera->getWorldOrientation());
91            }
92            else
93                COUT(3) << "NewHumanController, Warning: Using a ControllableEntity without Camera" << std::endl;
94        }
95    }*/
96   
97    void NewHumanController::doFire(unsigned int firemode)
98    {
99        //if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) {
100
101/*
102        // Get results, create a node/entity on the position
103        for ( itr = result.begin(); itr != result.end(); itr++ )
104        {
105            if (itr->movable && itr->movable->getName() == "Head")
106            {
107                soundMgr->StopSound( &jaguarSoundChannel );
108                soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel );
109                break;
110            } // if
111        }
112*/
113
114        HumanController::localController_s->getControllableEntity()->fire(firemode);
115    }
116
117    Vector3 NewHumanController::getTarget()
118    {
119        Ogre::RaySceneQuery * rsq = HumanController::localController_s->getControllableEntity()->getScene()->getSceneManager()->createRayQuery(Ogre::Ray());
120
121        //std::cout << "X: " << static_cast<float>(this->currentYaw_)/2*-1+.5 << "  Y: " << static_cast<float>(this->currentPitch_)/2*-1+.5 << endl;
122
123        Ogre::Ray mouseRay = HumanController::localController_s->getControllableEntity()->getCamera()->getOgreCamera()->getCameraToViewportRay(static_cast<float>(this->currentYaw_)/2*-1+.5, static_cast<float>(this->currentPitch_)/2*-1+.5);
124
125        rsq->setRay(mouseRay);
126        rsq->setSortByDistance(true);
127
128        /*
129        Distance of objects:
130        ignore everything under 200 maybe even take 1000 as min distance to shoot at
131
132        shots are regularly traced and are entities!!!!!!!!! this is the biggest problem
133        they vanish only after a distance of 10'000
134        */
135
136
137        Ogre::RaySceneQueryResult& result = rsq->execute();
138
139        Ogre::RaySceneQueryResult::iterator itr;
140        for (itr = result.begin(); itr != result.end(); ++itr)
141        {
142            //std::cout << "distance: " << itr->distance << "  name: " << itr->movable->getName() << " type: " << itr->movable->getMovableType();
143            if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 500)
144            {
145                // Try to cast the user pointer
146                WorldEntity* wePtr = dynamic_cast<WorldEntity*>(itr->movable->getUserObject());
147                if (wePtr)
148                {
149                    BaseObject* creator = wePtr->getCreator();
150                    if (this->targetMask_.isExcluded(creator->getIdentifier()))
151                        continue;
152                }
153                //std::cout << "  TAGGED";
154                itr->movable->getParentSceneNode()->showBoundingBox(true);
155                std::cout << itr->movable->getParentSceneNode()->_getDerivedPosition() << endl;
156                return itr->movable->getParentSceneNode()->_getDerivedPosition();
157            }
158            //std::cout << endl;
159        }
160
161        //if (result.front().movable->isInScene()) std::cout << "in scene" << endl;
162        // && result.front().movable->getParentSceneNode() != NULL) result.front().movable->getParentSceneNode()->showBoundingBox(true);
163        //result.front().movable->setVisible(false);
164
165        //std::cout << endl;
166/*
167        if (!result.empty()) {
168            Ogre::RaySceneQueryResultEntry obj = result.front();
169            std::cout << "distance: " << obj.distance << "  name: " << obj.movable->getName() << endl;
170        }
171*/
172        return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getWorldOrientation() * Vector3::NEGATIVE_UNIT_Z * 800);
173        //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getCamera()->getOgreCamera()->getOrientation() * Vector3::NEGATIVE_UNIT_Z);
174    }
175
176    void NewHumanController::yaw(const Vector2& value)
177    {
178//         SUPER(NewHumanController, yaw, value);
179        HumanController::yaw(value);
180       
181        this->currentYaw_ = value.x;
182        //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl;
183    }
184
185    void NewHumanController::pitch(const Vector2& value)
186    {
187//         SUPER(NewHumanController, pitch, value);
188        HumanController::pitch(value);
189       
190        this->currentPitch_ = value.x;
191        //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl;
192    }
193}
Note: See TracBrowser for help on using the repository browser.