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