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