Skip to content
Martin Prout edited this page Nov 22, 2017 · 10 revisions

Examples

attr_reader :drawing_mode, :points, :rot_x, :rot_y, :vertices, :renderer

def setup
  sketch_title 'Drawolver'  
  @renderer = AppRender.new(self)
  frame_rate 30
  reset_scene
end

def draw
  background 0
  unless drawing_mode
    translate(width / 2, height / 2)
    rotate_x rot_x
    rotate_y rot_y
    @rot_x += 0.01
    @rot_y += 0.02
    translate(-width / 2, -height / 2)
  end
  no_fill
  stroke 255
  points.each_cons(2) { |ps, pe| line ps.x, ps.y, pe.x, pe.y }
  unless drawing_mode
    stroke 125
    fill 120
    lights
    ambient_light 120, 120, 120
    vertices.each_cons(2) do |r1, r2|
      begin_shape(TRIANGLE_STRIP)
      r1.zip(r2).each do |v1, v2|
        v1.to_vertex(renderer)
        v2.to_vertex(renderer)
      end
      end_shape
    end
  end
end

def settings
  size 1024, 768, P3D
end

def reset_scene
  @drawing_mode = true
  @points = []
  @rot_x = 0.0
  @rot_y = 0.0
end

def mouse_pressed
  reset_scene
  points << Vec3D.new(mouse_x, mouse_y)
end

def mouse_dragged
  points << Vec3D.new(mouse_x, mouse_y)
end

def mouse_released
  points << Vec3D.new(mouse_x, mouse_y)
  recalculate_shape
end

def recalculate_shape
  @vertices = []
  points.each_cons(2) do |ps, _pe_|
    b = (points.last - points.first).normalize!
    a = ps - points.first
    dot_product = a.dot b
    b *= dot_product
    normal = points.first + b
    c = ps - normal
    vertices << []
    (0..360).step(12) do |ang|
      e = normal + c * DegLut.cos(ang)
      e.z = c.mag * DegLut.sin(ang)
      vertices.last << e
    end
  end
  @drawing_mode = false
end

Description

A class to describe a three dimensional vector, specifically a Euclidean (also known as geometric) vector. A vector is an entity that has both magnitude and direction. The datatype, however, stores the components of the vector (x, y, z). The magnitude and direction can be accessed via the methods mag and heading.

In many of the JRubyArt examples, you will see Vec3D used to describe a position, velocity, or acceleration. For example, if you consider a rectangle moving across the screen, at any given instant it has a position (a vector that points from the origin to its location), a velocity (the rate at which the object's position changes per time unit, expressed as a vector), and acceleration (the rate at which the object's velocity changes per time unit, expressed as a vector). Since vectors represent groupings of values, we cannot simply use traditional addition/multiplication/etc. Instead, we do some vector math, which is made easy by the methods inside the Vec3D class (+, -, /, * are used nevertheless).

Fields

x The x component of the vector

y The y component of the vector

z The z component of the vector

Methods

set Set the components of the vector

copy, dup Returns a copy of the vector

mag Returns the magnitude of the vector

mag_squared Returns the magnitude squared of the vector

+ Adds x, and y components to a vector, one vector to another

- Subtract x, and y components from a vector, one vector to another

* Multiply a vector by a scalar

/ Divide a vector by a scalar

== equals values

= assignment can be used in combination eg *= to set vector the result of scalar multiply

eqls? equals values

equals identity equals

dist Calculate the distance between two points

dot Calculate the dot product of two vectors

cross Calculate and return the cross product

normalize Returns copy of vector normalized to a length of 1

normalize! Normalize the vector to a length of 1

limit Limit the magnitude of the vector

set_mag set the vector magnitude to a given scalar (accepts an optional block, which if given should evaluate to a boolean to allow conditional setting to eg max velocity)

lerp Return a new vector a linear interpolation of a vector to another vector

lerp! Linear interpolate the vector to another vector

angle_between Calculate and return the angle between two vectors

to_a Returns a representation of the vector as an array of Float

inspect Returns to_s

to_s Returns a representation of the vector as a string

to_vertex See drawolver.rb example for usage, sends vector to processing vertex, takes a renderer arg.

to_normal See trefoil.rb example sends vector to processing normal, uses a ShapeRenderer.

to_vertex_uv See trefoil.rb example, uses a ShapeRenderer.

Constructor

def initialize(x = 0, y = 0, z = 0)