Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Tuesday, 3 May 2011

Winged Edge Mesh and Physics from Toxiclibs

Here is the toxiclibs InflateMesh verlet physics example demonstrating winged mesh class functionality. Although this was a P3D sketch in the example it looks best using OPENGL. Because there is still an issue with linux and ruby-processing OPENGL at less than full_screen, I have scaled some of the variables in the sketch (mainly annotated I think), anyway you should probably look at the original (included in the library download toxiclibs-complete version 0020).

# <p>This example uses the attraction behavior to inflate a 3D mesh.
# The mesh vertices are re-created as physics particles and connected
# using springs. Upon mouse press the inflation force is applied,
# counteracting the forces created by the springs, causing the mesh to
# expand and deform.</p>
# <p>Usage: Click and hold mouse button to inflate mesh</p>

# Copyright (c) 2010 Karsten Schmidt translated to ruby processing by Martin Prout (Spring 2011)
# This demo & library is free software you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation either
# version 2.1 of the License, or (at your option) any later version.
# http:#creativecommons.org/licenses/LGPL/2.1/
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
class InflateMesh < Processing::App
  load_libraries 'toxiclibs_p5', 'toxiclibscore', 'verletphysics', 'opengl'
  include_package 'processing.opengl'
  include_package 'toxi.geom'
  include_package 'toxi.geom.mesh.subdiv'
  include_package 'toxi.geom.mesh'
  include_package 'toxi.physics'
  include_package 'toxi.physics.behaviors'
  include_package 'toxi.physics.constraints'
  include_package 'toxi.processing'

  attr_reader :physics, :inflate, :box, :gfx

  def setup
    @gfx = ToxiclibsSupport.new(self)

  def draw
    for vert in box.vertices.values
    box.center(nil)              # nil means centre at 0, 0, 0
    for vert in box.vertices.values
    translate(width / 2, height / 2, 0)
    rotate_x((height / 2 - mouse_y) * 0.01)
    rotate_y((width / 2 - mouse_x) * 0.01)
    directional_light(255, 255, 255, -200, 1000, 500)
    @gfx.origin(Vec3D.new, 300)  # scaled up axes display from 50
    @gfx.mesh(box, true, 10)     # scaled up normals display from 5

  def init_physics
    @box = WETriangleMesh.new
    # create a simple start mesh
    #box.add_mesh(Cone.new(Vec3D.new(0, 0, 0), Vec3D.new(0, 1, 0), 10, 50, 100).to_mesh(4))
    @box.add_mesh(AABB.new(Vec3D.new, 150).to_mesh)      # scaled up from 50
    # then subdivide a few times...
    @physics = VerletPhysics.new
    @physics.set_world_bounds(AABB.new(Vec3D.new, 540))  # scaled up from 180
    # turn mesh vertices into physics particles
    for vert in box.vertices.values
    # turn mesh edges into springs
    for w_edge in box.edges.values
      vp_a = physics.particles.get(w_edge.a.id)
      vp_b = physics.particles.get(w_edge.b.id)
      @physics.add_spring(VerletSpring.new(vp_a, vp_b, vp_a.distance_to(vp_b), 0.05))

  def key_pressed     # using a switch anticipating more key actions
    case key
    when 'i'
    when 's'
    save_frame "inflate.png"

  def mouse_pressed
    @inflate=AttractionBehavior.new(Vec3D.new, 400, -0.3, 0.001)

  def mouse_released

  def setup_opengl
    render_mode OPENGL
    hint ENABLE_OPENGL_4X_SMOOTH     # optional

No comments:

Post a Comment


About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby- and processing-3.2.2