Debian Codegraph @

utils/fgai/AIObject.cxx

raw symbols SCIP
1 // Copyright (C) 2009 - 2012 Mathias Froehlich
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include "AIObject.hxx"
22
23 #include <simgear/bvh/BVHLineSegmentVisitor.hxx>
24 #include <simgear/bvh/BVHNode.hxx>
25 #include <simgear/math/SGGeometry.hxx>
26
27 #include "AIManager.hxx"
28
29 namespace fgai {
30
31 AIObject::AIObject() :
32 _environment(new AIEnvironment),
33 _subsystemGroup(new AISubsystemGroup)
34 {
35 }
36
37 AIObject::~AIObject()
38 {
39 }
40
41 void
42 AIObject::init(AIManager& manager)
43 {
44 _simTime = manager.getSimTime();
45 }
46
47 void
48 AIObject::update(AIManager& manager, const SGTimeStamp& simTime)
49 {
50 _simTime = simTime;
51 }
52
53 void
54 AIObject::shutdown(AIManager& manager)
55 {
56 _simTime = SGTimeStamp();
57 }
58
59 void
60 AIObject::setGroundCache(const AIPhysics& physics, AIBVHPager& pager, const SGTimeStamp& dt)
61 {
62 SGVec3d point = physics.getLocation().getPosition();
63 double linearVelocity = norm(physics.getLinearBodyVelocity());
64 // The 2 is a security factor for accelerations, but at least 100 meters
65 double radius = std::max(100.0, 2*dt.toSecs()*linearVelocity);
66 SGSphered requiredSphere(point, radius);
67 /// Are we already good enough?
68 if (requiredSphere.inside(_querySphere))
69 return;
70 // Now query something somehow bigger to avoid querying again in the next frame
71 SGSphered sphere(point, 4*radius);
72 _node = pager.getBoundingVolumes(sphere);
73 if (!_node.valid())
74 return;
75 _querySphere = sphere;
76 }
77
78 bool
79 AIObject::getGroundIntersection(SGVec3d& point, SGVec3d& normal, const SGLineSegmentd& lineSegment) const
80 {
81 if (!_node.valid())
82 return false;
83 simgear::BVHLineSegmentVisitor lineSegmentVisitor(lineSegment);
84 _node->accept(lineSegmentVisitor);
85 if (lineSegmentVisitor.empty())
86 return false;
87 normal = lineSegmentVisitor.getNormal();
88 point = lineSegmentVisitor.getPoint();
89 return true;
90 }
91
92 bool
93 AIObject::getGroundIntersection(SGPlaned& plane, const SGLineSegmentd& lineSegment) const
94 {
95 SGVec3d point;
96 SGVec3d normal;
97 if (!getGroundIntersection(point, normal, lineSegment))
98 return false;
99 plane = SGPlaned(normal, point);
100 return true;
101 }
102
103 } // namespace fgai