Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10226 was 10226, checked in by tfahrni, 17 years ago
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 "npcs/npc_test.h"
24
25#include "shell_command.h"
26SHELL_COMMAND(setDistanceToPlayer, MovementModule, setDistanceToPlayer);
27SHELL_COMMAND(setDistanceToNPC, MovementModule, setDistanceToNPC);
28SHELL_COMMAND(setMaxAccleartion, MovementModule, setMaxAccleartion);
29SHELL_COMMAND(setTestValue, MovementModule, setTestValue);
30SHELL_COMMAND(setTestValue2, MovementModule, setTestValue2);
31
32float MovementModule::distanceToPlayer=15;
33float MovementModule::distanceToNPC=2;
34float MovementModule::maxAccleration=300.0f;
35float MovementModule::testValue=2;
36float MovementModule::testValue2=40;
37
38void MovementModule::setDistanceToPlayer(float newValue){ distanceToPlayer=newValue; }
39void MovementModule::setDistanceToNPC(float newValue){ distanceToNPC=newValue; }
40void MovementModule::setMaxAccleartion(float newValue){ maxAccleration=newValue; }
41void MovementModule::setTestValue(float newValue){ testValue=newValue; }
42void MovementModule::setTestValue2(float newValue){ testValue2=newValue; }
43
44MovementModule::MovementModule()
45{
46        tickCount=0;
47        randomFreq=100;
48}
49
50
51void MovementModule::process(float dt)
52{
53        if(npc == NULL)return;
54
55        Vector tmpVector;
56        float tmpFloat;
57        Vector npcCollision;
58        Vector playerCollision;
59
60        weight=1;
61
62
63        //get information about player
64        Player* pl = State::getPlayer();
65        if( pl == NULL)return;
66        Vector playerPosition = pl->getPlayable()->getAbsCoor();
67        float playerRadius=getRadius( pl->getPlayable() );
68
69
70        //get information about myself
71        Vector myPosition = npc->getAbsCoor();
72        float myRadius = getRadius(npc);
73
74
75        float aMax=maxAccleration;
76        float vMax=800.0f/myRadius;
77
78
79        //anti player collision
80        Vector vectorToPlayer = playerPosition - myPosition;
81
82        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
83        if(tmpFloat<0.1)tmpFloat=0.1;
84        playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1);
85
86
87        //anti NPC collision
88        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
89        {
90                if(*it==npc)continue;
91
92                tmpVector=myPosition-(*it)->getAbsCoor();
93                tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it);
94
95                if(tmpFloat<0.1)tmpFloat=0.1;
96                tmpVector=tmpVector/(tmpFloat*tmpFloat);
97
98                npcCollision=npcCollision+tmpVector;
99        }
100
101
102        //calculate correction vector
103        Vector vectorToDestination=destination-myPosition;
104
105        Vector correction=              playerCollision*50*3
106                                                                +       npcCollision*50*3
107                                                                +       Vector(0,0,0)
108                                                                +       destinationMovement*2//-movement
109                                                                +       (vectorToDestination-movement)*3;
110
111        correction.y=0;
112
113
114        //limit accleration
115        float correctionLen=correction.len();
116        if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
117        movement+=correction;
118
119
120        //limit speed
121        float movementLen=movement.len();
122        if(movementLen>vMax)movement=movement/movementLen*vMax;
123
124
125        //move NPC...
126        npc->shiftCoor(movement * dt);
127
128
129        //rotate NPC
130        view = movement.cross( Vector(0,1,0) ).getNormalized();
131        npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3);
132
133}
134
135
Note: See TracBrowser for help on using the repository browser.