Sha256: af0df92462713937d92eb7db964376571a7295efed6abb6eecc7c14262bb2f0e
Contents?: true
Size: 1.84 KB
Versions: 3
Compression:
Stored size: 1.84 KB
Contents
# Cubes Contained Within a Cube # by Ira Greenberg. # # Collision detection against all outer cube's surfaces. # Uses the PVector and Cube classes. # fjenett, 2010-03-12: did some cleanups and rubyfication here load_library 'cube' def setup size 640, 360, P3D lights smooth 4 @cube_count = 20 @cubes = [] 0.upto( @cube_count ) { |i| cube_size = rand(5 .. 15) c = Cube.new(cube_size, cube_size, cube_size) c.position = PVector.new(0.0, 0.0, 0.0) c.speed = PVector.new(rand(-1 .. 1), rand(-1 .. 1), rand(-1 .. 1)) c.rotation = PVector.new(rand(40 .. 100), rand(40 .. 100), rand(40 .. 100)) @cubes << c } @cube_colors = [ color(0), color(51), color(102), color(153), color(204), color(255) ] @cube_colors.reverse @stage_size = 300 @stage = Cube.new @stage_size, @stage_size, @stage_size end def draw background 50 translate width/2, height/2, -130 rotate_x frame_count * 0.001 rotate_y frame_count * 0.002 rotate_z frame_count * 0.001 no_fill stroke 255 @stage.draw @cubes.each_with_index { |c, i| # draw cube push_matrix translate c.position.x, c.position.y, c.position.z fcpi = frame_count * PI rotate_x fcpi / c.rotation.x rotate_y fcpi / c.rotation.y rotate_x fcpi / c.rotation.z no_stroke c.draw @cube_colors pop_matrix # move it c.position.add c.speed # draw lines if i > 0 stroke 0 c2 = @cubes[i-1] line c.position.x, c.position.y, c.position.z, c2.position.x, c2.position.y, c2.position.z end # collision s2 = @stage_size / 2 c.speed.x *= -1 if (c.position.x / s2).abs > 1 # note that in Ruby abs(-12) is -12.abs c.speed.y *= -1 if (c.position.y / s2).abs > 1 c.speed.z *= -1 if (c.position.z / s2).abs > 1 } end
Version data entries
3 entries across 3 versions & 1 rubygems