forked from bjorn/blipnblup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flyer.cpp
executable file
·82 lines (71 loc) · 2.82 KB
/
flyer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Blip 'n Blup
// Copyright (C) 2016 LucKey Productions (luckeyproductions.nl)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// Commercial licenses are available through frode@lindeijer.nl
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "flyer.h"
#include "catchable.h"
#include "castmaster.h"
Flyer::Flyer(Context* context) : Controllable(context),
altitude_{2.0f},
flyHeight_{2.3f + randomizer_}
{
}
void Flyer::OnNodeSet(Node* node)
{
Controllable::OnNodeSet(node);
rigidBody_->SetLinearRestThreshold(0.01f);
rigidBody_->SetLinearDamping(0.5f);
rigidBody_->SetRestitution(0.23f);
rigidBody_->SetAngularDamping(1.0f);
rigidBody_->SetRollingFriction(0.5f);
rigidBody_->SetAngularFactor(Vector3::UP);
}
void Flyer::Update(float timeStep)
{
Controllable::Update(timeStep);
if (node_->HasComponent<Catchable>()
&& node_->GetComponent<Catchable>()->IsCaught())
return;
//Hover
rigidBody_->ApplyForce(Vector3::UP * flyThrust_ * 10.0f *
pow(altitude_ - node_->GetPosition().y_ - rigidBody_->GetLinearVelocity().y_ * 0.1f, 0.5f) *
MC->Sine(1.0f - 0.23f * randomizer_, 1.0f, 2.0f, randomizer_) * timeStep);
//Fly
if (rigidBody_->GetLinearVelocity().Length() < maxFlySpeed_
|| (rigidBody_->GetLinearVelocity().Normalized() + move_.Normalized()).Length() < 1.0f)
{
Vector3 velocity{rigidBody_->GetLinearVelocity()};
Vector3 force{move_ * flyThrust_};
float remainingFlySpeed{maxFlySpeed_ - velocity.Length() * (1.0f-(velocity.Angle(force)/90.0f))};
float maxThrust = Max(flyThrust_ * remainingFlySpeed, 0.0f);
if (force.Length() > maxThrust)
force = force.Normalized() * maxThrust;
rigidBody_->ApplyForce(force * timeStep);
}
CorrectAltitude();
AlignWithMovement(timeStep);
}
void Flyer::CorrectAltitude()
{
PODVector<PhysicsRaycastResult> results{};
Ray floorRay(node_->GetPosition()
+ Vector3::UP * 5.0f,
Vector3::DOWN);
GetSubsystem<CastMaster>()->PhysicsRayCast(results, floorRay, 1024.0f, LAYER(0));
if (results.Size())
altitude_ = results[0].position_.y_ + flyHeight_;
}