From 0583a374f88f0654a875c07aaa051e189bb50125 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 5 Jun 2018 13:18:20 +0530 Subject: [PATCH 1/4] Add Turtle Class A 3-D turtle with yaw, pitch and roll --- src/PiCraft.jl | 2 ++ src/turtle.jl | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/turtle.jl diff --git a/src/PiCraft.jl b/src/PiCraft.jl index a5b5b20..ee57e5b 100644 --- a/src/PiCraft.jl +++ b/src/PiCraft.jl @@ -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 diff --git a/src/turtle.jl b/src/turtle.jl new file mode 100644 index 0000000..b34b399 --- /dev/null +++ b/src/turtle.jl @@ -0,0 +1,71 @@ +""" + turtle + +A graphics turtle meant to move in all 3-Dimensions. It contains the following fields: + +* `pos` : The coordinates of the turtle. +* `direction` : The direction in which the turtle is facing. +* `normal` : The 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`. + +""" +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 + + function turtle() + pos = getPos() + direction = [1.0; 0.0; 0.0] + normal = [0.0; -1.0; 0.0] + penBlock = Block(41) + stepSize = 0.3 + penDown = true + return new(pos, direction, normal, penBlock, stepSize, penDown) + end +end + +""" + move(t::turtle, s::Int) + +Move the turtle `t` forward `s` steps. +""" +function move(t::turtle, s::Int) + 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 From fdbd3626a528cf38a9d673bf25b0e35fd14677d4 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Jun 2018 18:44:28 +0530 Subject: [PATCH 2/4] Allow Real for steps then convert to Integer --- src/turtle.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/turtle.jl b/src/turtle.jl index b34b399..30a8cf2 100644 --- a/src/turtle.jl +++ b/src/turtle.jl @@ -35,7 +35,8 @@ end Move the turtle `t` forward `s` steps. """ -function move(t::turtle, s::Int) +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) From 327c7b203a04806f0be4d579a44a9cebfcc501bc Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Jun 2018 19:09:26 +0530 Subject: [PATCH 3/4] Added keywork constructor --- src/turtle.jl | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/turtle.jl b/src/turtle.jl index 30a8cf2..c49bb58 100644 --- a/src/turtle.jl +++ b/src/turtle.jl @@ -19,15 +19,7 @@ mutable struct turtle stepSize::Float64 penDown::Bool - function turtle() - pos = getPos() - direction = [1.0; 0.0; 0.0] - normal = [0.0; -1.0; 0.0] - penBlock = Block(41) - stepSize = 0.3 - penDown = true - return new(pos, direction, normal, penBlock, stepSize, penDown) - end + 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 """ From 2aae5cd1bbf5581b1fab92e0a047761a681a08c8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 6 Jun 2018 20:28:36 +0530 Subject: [PATCH 4/4] docs changes --- src/turtle.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/turtle.jl b/src/turtle.jl index c49bb58..6fcc55b 100644 --- a/src/turtle.jl +++ b/src/turtle.jl @@ -4,12 +4,14 @@ A graphics turtle meant to move in all 3-Dimensions. It contains the following fields: * `pos` : The coordinates of the turtle. -* `direction` : The direction in which the turtle is facing. -* `normal` : The direction normal to the turtle's body. +* `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}