Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed segfault

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=250.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        WorldEntity* playable = pl->getPlayable();
67        if( playable == NULL)return;
68
69        Vector playerPosition = pl->getPlayable()->getAbsCoor();
70        float playerRadius=getRadius( pl->getPlayable() );
71
72        //get information about myself
73        Vector myPosition = npc->getAbsCoor();
74        float myRadius = getRadius(npc);
75
76
77        float aMax=maxAccleration;
78        float vMax=800.0f/myRadius;
79
80
81        //anti player collision
82        Vector vectorToPlayer = playerPosition - myPosition;
83
84        tmpFloat=vectorToPlayer.len()-playerRadius-myRadius-distanceToPlayer;
85        if(tmpFloat<0.1)tmpFloat=0.1;
86        playerCollision=vectorToPlayer/(tmpFloat*tmpFloat)*(-1);
87
88
89        //anti NPC collision
90        for(ObjectList<WorldEntity>::const_iterator it = WorldEntity::objectList().begin(); it != WorldEntity::objectList().end(); ++it)
91        {
92                if(*it==npc)continue;
93
94                tmpVector=myPosition-(*it)->getAbsCoor();
95                tmpFloat=tmpVector.len()-myRadius-distanceToNPC-getRadius(*it);
96
97                if(tmpFloat<0.1)tmpFloat=0.1;
98                tmpVector=tmpVector/(tmpFloat*tmpFloat);
99
100                npcCollision=npcCollision+tmpVector;
101        }
102
103
104        //calculate correction vector
105        Vector vectorToDestination=destination-myPosition;
106
107        Vector correction=              playerCollision*50*3
108                                                                +       npcCollision*50*3 *6/myRadius
109                                                                +       Vector(0,0,0)
110                                                                +       destinationMovement*2//-movement
111                                                                +       (vectorToDestination-movement)*3;
112
113        correction.y=0;
114
115
116        //limit accleration
117        float correctionLen=correction.len();
118        if(correctionLen>aMax*dt)correction=correction/correctionLen*aMax*dt;
119        movement+=correction;
120
121
122        //limit speed
123        float movementLen=movement.len();
124        if(movementLen>vMax)movement=movement/movementLen*vMax;
125
126
127        //move NPC...
128        npc->shiftCoor(movement * dt);
129
130
131        //rotate NPC
132        view = movement.cross( Vector(0,1,0) ).getNormalized();
133        npc->setAbsDirSoft( Quaternion( view, Vector(0,1,0)),3);
134
135}
136
137
Note: See TracBrowser for help on using the repository browser.