-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoat.cs
More file actions
180 lines (153 loc) · 5.02 KB
/
Boat.cs
File metadata and controls
180 lines (153 loc) · 5.02 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Boatanator
{
class Boat : RigidBody
{
static Model model;
static Matrix local;
Matrix world;
//Verlet verlet = new Verlet();
public string nickname = "haha";
public string host = "";
public Color color = Color.DeepSkyBlue;
public Boat()
{
float L = 2, W = 1, H = 1, D = 0.5f;
var vs = new Verlet[]
{
new Verlet(L,0,W),
new Verlet(L,0,-W),
new Verlet(-L,0,-W),
new Verlet(-L,0, W),
new Verlet(0,H,0),
new Verlet(-L,-D,0)
};
Init(vs);
}
public void Load(ContentManager content)
{
model = content.Load<Model>("boat");
local = Matrix.CreateScale(0.3f)*Matrix.CreateRotationY(MathHelper.Pi);
}
public void Draw(Camera cam)
{
world = Matrix.CreateWorld(Position, Direction, Up);
model.Draw(local*world, cam.View, cam.Projection);
}
public void Step(bool w, bool s, bool a, bool d)
{
var dir = Direction;
var up = Up;
var right = Vector3.Cross(dir, Up);
///////////////////////////////////////
// Gravity
///////////////////////////////////////
float g = 8;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i].A = new Vector3(0, -g, 0);
}
///////////////////////////////////////
// Lift
///////////////////////////////////////
for (int i = 0; i < 4; i++)
{
if (vertices[i].Pos.Y < 0)
vertices[i].A += new Vector3(0, Math.Max(0, Math.Min(20, (1.3f-vertices[i].Pos.Y) * g)), 0);
}
///////////////////////////////////////
// Friction
///////////////////////////////////////
for (int i = 0; i < 4; i++)
{
if (vertices[i].Pos.Y < 0)
{
vertices[i].AddSqFriction(dir, 0.001f);
vertices[i].AddSqFriction(right, 0.1f);
vertices[i].AddSqFriction(up, 1f);
}
}
///////////////////////////////////////
// Control
///////////////////////////////////////
float strength = 20;
float small = 2;
Vector3 acc = Vector3.Zero;
if (vertices[5].Pos.Y < 0)
{
if (w)
{
if (d)
{
acc = Vector3.Normalize(dir - right) * strength;
}
else if (a)
{
acc = Vector3.Normalize(dir + right) * strength;
}
else
{
acc = dir * strength;
}
}
else
{
if (d)
{
acc = -right * small;
}
else if (a)
{
acc = right * small;
}
else if (s)
{
acc = -dir * strength / 2;
}
}
vertices[5].A += acc;
}
base.Step();
}
public Vector3 Position { get { return (vertices[0].Pos + vertices[2].Pos) * 0.5f; } }
public Vector3 Direction { get { return Vector3.Normalize(vertices[0].Pos - vertices[3].Pos); } }
public Vector3 Up { get { return Vector3.Normalize(2*vertices[4].Pos - vertices[2].Pos - vertices[0].Pos); } }
public int Serialize(byte[] buffer, int p)
{
int op = p;
foreach (var v in vertices)
{
p += v.Serialize(buffer, p);
}
int offset = p;
p += 1;
p += Encoding.UTF8.GetBytes(nickname, 0, nickname.Length, buffer, p);
buffer[offset] = (byte)(p - offset - 1);
buffer[p++] = color.R;
buffer[p++] = color.G;
buffer[p++] = color.B;
return p - op;
}
public int Deserialize(byte[] buffer, int p)
{
int op = p;
foreach (var v in vertices)
{
p += v.Deserialize(buffer, p);
}
byte len = buffer[p++];
nickname = Encoding.UTF8.GetString(buffer, p, len);
p += len;
color.R = buffer[p++];
color.G = buffer[p++];
color.B = buffer[p++];
return p - op;
}
}
}