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

Tuesday, 2 February 2010

The Best of Both Worlds (combining PeasyCam and control_panel

This ruby processing sketch shows how to get the best of both worlds; the smooth easily setup PeasyCam, that you can use the scroll-wheel to zoom, and mouse to drag, and the possible fine grain control of a control panel. In this example I have initially disabled mouse control; use the button to toggle mouse control on or off. Use the control panel slider to set off the rotation or the freeze! button (to stop the rotation). If you only want fine grain rotation (cf. continuous rotation with the slider) then you will need get the camera to remember state (I believe Quark has done this in vanilla processing). If you are really keen you could also implement a slider to control the zoom of the peasy cam (in place of the mouse-wheel).

load_libraries 'PeasyCam', 'control_panel'
import 'peasy'

attr_reader :cam, :x_rotate, :y_rotate, :z_rotate, :controlled

def setup()
  size(200, 200, P3D)
  configure_panel()
  @controlled = true
  configure_camera()
end

def configure_camera()    
  @cam = PeasyCam.new(self, 100)
  cam.set_minimum_distance(50)
  cam.set_maximum_distance(500)
  mouse_control()
end

def configure_panel()
  control_panel do |c|
    c.slider(:x_rotate, -1.0..1.0, 0.0)
    c.slider(:y_rotate, -1.0..1.0, 0.0)
    c.slider(:z_rotate, -1.0..1.0, 0.0)
    c.button(:freeze!)
    c.button(:mouse_control)
  end
end

def freeze!()
  @x_rotate = 0.0
  @y_rotate = 0.0
  @z_rotate = 0.0
end

def mouse_control()     # toggle mouse controlled camera
  cam.set_mouse_controlled(!controlled)
  @controlled = !controlled
end

def draw()
  cam.rotate_x(x_rotate/100)
  cam.rotate_y(y_rotate/100)
  cam.rotate_z(z_rotate/100)
  rotate_x(-0.5)
  rotate_y(-0.5)
  background(0)
  fill(255, 0, 0)
  box(30)
  push_matrix()
  translate(0, 0, 20)
  fill(0, 0, 255)
  box(5)
  pop_matrix()
end




No comments:

Post a Comment

Followers

About Me

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