-
Notifications
You must be signed in to change notification settings - Fork 0
/
rigidbody.jl
34 lines (30 loc) · 924 Bytes
/
rigidbody.jl
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
mutable struct RigidBody
# where rocket currently is
position::Vector{Float64}
# direction and speed of motion
velocity::Vector{Float64}
# sum of all forces working on rocket
force::Vector{Float64}
# angle rocket is pointing and dirction of thrust
orientation::Float64
# changes as fuel burns and stages ejected
mass::Float64
end
# constructor with simplifying assumptions
function RigidBody(mass::Real, force::Real)
p = [0, 0]
v = [0, 0]
F = [force, 0]
θ = 0
RigidBody(p, v, F, θ, mass)
end
# convenience functions
force(body::RigidBody) = body.force
mass(body::RigidBody) = body.mass
acceleration(body::RigidBody) = force(body) / mass(body)
# explicit euler method numerical approxemation to an integral
function integrate!(body::RigidBody, Δt::Number)
body.position += body.velocity * Δt
body.velocity += acceleration(body) * Δt
body
end