Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/ai/src/ai/movement_module.cc @ 10075

Last change on this file since 10075 was 10075, checked in by tfahrni, 17 years ago

AI can move without collisions

File size: 3.5 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Thomas Fahrni
13   co-programmer:
14*/
15#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_AI
16
17#include "movement_module.h"
18#include "ai_engine.h"
19#include "state.h"
20#include "debug.h"
21#include "player.h"
22#include "playable.h"
23#include "aabb.h"
24#include "npcs/npc_test.h"
25
26#include "shell_command.h"
27SHELL_COMMAND(aiacc, MovementModule, setAccleration);
28
29
30//class AIEngine;
31
32MovementModule::MovementModule(){
33        //MovementModule::aa=100.0f;
34        //a=100.0f;
35        //std::cout << "MovementModule created...\n";
36}
37
38MovementModule::~MovementModule(){
39}
40
41
42void MovementModule::setAccleration(float newValue)
43{
44        std::cout << "Setting a to: "<< newValue << "\n";
45        //MovementModule::aa=newValue;
46}
47
48
49
50
51float MovementModule::getSize(WorldEntity* object)
52{
53        //does not work...
54        AABB* aabb = object->getModelAABB();
55        Vector a = aabb->getAxisX();
56        Vector b = aabb->getAxisY();
57        Vector c = aabb->getAxisZ();
58
59        float da=a.len();
60        float db=b.len();
61        float dc=c.len();
62        float size;
63
64        if(da>db){
65                size=(dc>da)?dc:da;
66        }else{
67                size=(dc>db)?dc:db;
68        }
69
70        return size;
71}
72
73
74
75
76void MovementModule::process()
77{
78        float dt=AIEngine::getInstance()->dtS;
79        if( owner == NULL) return;
80        Vector myAbsPos = owner->getAbsCoor();
81
82        //PRINTF(0)(" NPC abs coor: %f, %f, %f\n", myAbsPos.x, myAbsPos.y, myAbsPos.z);
83
84        Player* pl = State::getPlayer();
85        if( pl == NULL) return;
86        Vector playerAbsPos = pl->getPlayable()->getAbsCoor();
87
88        //PRINTF(0)(" Player abs coor: %f, %f, %f\n", playerAbsPos.x, playerAbsPos.y, playerAbsPos.z);
89
90        ////////////
91
92        //if(aa<0)aa=100.0f;
93        float a=200.0f;
94        float vMax=200.0f;
95       
96        //float keepDist= this->getSize(owner);//+ this->getSize(pl->getPlayable());
97        //std::cout << "Distance: " << keepDist << "\n";
98
99
100        Vector vectorToPlayer = playerAbsPos - myAbsPos;
101        float dist=vectorToPlayer.len();
102        //Vector nVectorToPlayer=vectorToPlayer/dist*(dist-keepDist);
103
104
105        // std::vector<Vector> collisonCorrection;
106        // get all npcs
107
108        Vector tmpNPCpos;
109        float tmpDist;
110        Vector tmpCorrectionVect;
111        Vector antiNPCCollision;
112
113        for (ObjectList<NPC2>::const_iterator it = NPC2::objectList().begin();
114                         it != NPC2::objectList().end();
115                         ++it)
116        {
117                if(*it==owner)continue;
118                tmpNPCpos=(*it)->getAbsCoor();
119                tmpCorrectionVect = myAbsPos-tmpNPCpos;
120               
121                tmpDist=tmpCorrectionVect.len()-16;
122                if(tmpDist<0.1)tmpDist=0.1;
123                tmpCorrectionVect=tmpCorrectionVect/(tmpDist*tmpDist);
124               
125                antiNPCCollision=antiNPCCollision+tmpCorrectionVect;
126        }
127
128
129
130        Vector antiPlayerCollision=vectorToPlayer*(-1);
131        tmpDist=antiPlayerCollision.len()-45;
132        if(tmpDist<0.1)tmpDist=0.1;
133        antiPlayerCollision=antiPlayerCollision/(tmpDist*tmpDist);
134       
135        Vector correction=antiPlayerCollision*50+antiNPCCollision*50+vectorToPlayer-v;
136        //Vector nCorrection=
137        correction.y=0;
138        float correctionLen=correction.len();
139        if(correctionLen>a*dt)correction=correction/correctionLen*a*dt;
140        v+=correction;
141
142        float vLen=v.len();
143        if(vLen>vMax)v/vLen*vMax;
144
145        //Move NPC...
146        owner->shiftCoor(v*dt);
147
148        //Rotate NPC
149        Vector view = v+correction;
150        //if(vectorToPlayer.dot(v)<0){
151        //      view = v.cross( Vector(0,1,0) ).getNormalized();
152        //}else{
153                view = v.cross( Vector(0,-1,0) ).getNormalized();
154        //}
155        //if(dist<keepDist)view=view*-1;
156        //owner->setAbsDir( Quaternion( view, Vector(0,1,0)));
157        owner->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),1.1);
158}
159
160
Note: See TracBrowser for help on using the repository browser.