Skip to content

Commit

Permalink
Add Turtle Class (#17)
Browse files Browse the repository at this point in the history
* Add Turtle Class

A 3-D turtle with yaw, pitch and roll

* Allow Real for steps then convert to Integer

* Added keywork constructor

* docs changes
  • Loading branch information
Ellipse0934 authored and aviks committed Jun 6, 2018
1 parent 686b989 commit f5fa345
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/PiCraft.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module PiCraft

include("blocks.jl")
include("turtle.jl")

export World, Block, connectToWorld, mc_send, getBlock, setBlock, setBlocks, getHeight, getPlayerIds
export setting, saveWorld, restoreWorld, post, getTile, setTile, getPos, setPos, pollBlockHits
export clearEvents, camera
export turtle, move, yaw, pitch, roll

type World
s::TCPSocket
Expand Down
66 changes: 66 additions & 0 deletions src/turtle.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
turtle
A graphics turtle meant to move in all 3-Dimensions. It contains the following fields:
* `pos` : The coordinates of the turtle.
* `direction` : The Roll Axis - direction in which the turtle is facing.
* `normal` : The Yaw axis - direction normal to the turtle's body.
* `penBlock` : The block which will be used to draw.
* `stepSize` : The size of a turtle's step.
* `penDown` : Activation state of `penBlock`.
The default constructor initializes the `turtle` at the player's position,roll axis is the positive x-direction,
with the yaw axis pointing downwards. `penBlock` is gold by default and is activated with the `stepSize` = 0.5
"""
mutable struct turtle
pos::Tuple{Float64, Float64, Float64}
direction::Array{Float64, 1} # Direction turtle is facing, Roll axis
normal::Array{Float64, 1} # Direction normal to the turtle's body, Yaw axis
penBlock::PiCraft.Block
stepSize::Float64
penDown::Bool

turtle(;pos = getPos(), direction = [1.0; 0.0; 0.0], normal = [0.0; -1.0; 0.0], penBlock = Block(41), stepSize = 0.5, penDown = true) = new(pos, direction, normal, penBlock, stepSize, penDown)
end

"""
move(t::turtle, s::Int)
Move the turtle `t` forward `s` steps.
"""
function move(t::turtle, s::Real)
s = floor(Int, s)
for i in 1:s
t.penDown && setBlock(t.pos, t.penBlock)
t.pos = t.pos .+ Tuple(t.direction*t.stepSize)
end
end

"""
pitch(t::turtle, θ::Real)
Pitch the turtle `t` by `θ` degrees.
"""
function pitch(t::turtle, θ::Real)
yawAxis = cross(t.normal, t.direction)
t.normal, t.direction = normalize(t.normal*cosd(θ) + cross(yawAxis, t.normal)*sind(θ)), normalize(t.direction*cosd(θ) + cross(yawAxis, t.direction)*sind(θ))
end

"""
yaw(t::turtle, θ::Real)
Yaw the turtle `t` by `θ` degrees.
"""
function yaw(t::turtle, θ::Real)
t.direction = normalize(t.direction*cosd(θ) + cross(t.normal, t.direction)*sind(θ))
end

"""
roll(t::turtle, θ::Real)
Roll the turtle `t` by `θ` degrees.
"""
function roll(t::turtle, θ::Real)
t.normal = normalize(t.normal*cosd(θ) + cross(t.direction, t.normal)*sind(θ))
end

0 comments on commit f5fa345

Please sign in to comment.