Sha256: 3cb6f350617851636d6ce1647b375a4ee0c9164723bb3aa4ec468c840674a245
Contents?: true
Size: 1.68 KB
Versions: 4
Compression:
Stored size: 1.68 KB
Contents
# The Nature of Code # Daniel Shiffman # http://natureofcode.com require 'forwardable' # A rectangular box class Box extend Forwardable def_delegators(:@app, :push_matrix, :pop_matrix, :fill, :rotate, :box2d, :stroke_weight, :translate, :rect_mode, :stroke, :rect) # We need to keep track of a Body and a width and height attr_reader :body, :w, :h # Constructor def initialize(x, y, w, h, lock) @w, @h = w, h @app = $app # Define and create the body bd = BodyDef.new bd.position.set(box2d.processing_to_world(Vec2.new(x, y))) bd.type = lock ? BodyType::STATIC : BodyType::DYNAMIC @body = box2d.createBody(bd) # Define the shape -- a (this is what we use for a rectangle) sd = PolygonShape.new box2dW = box2d.scale_to_world(w / 2) box2dH = box2d.scale_to_world(h / 2) sd.setAsBox(box2dW, box2dH) # Define a fixture fd = FixtureDef.new fd.shape = sd # Parameters that affect physics fd.density = 1 fd.friction = 0.3 fd.restitution = 0.5 body.createFixture(fd) # Give it some initial random velocity body.setLinearVelocity(Vec2.new(rand(-5..5),rand(2..5))) body.setAngularVelocity(rand(-5..5)) end # This function removes the particle from the box2d world def killBody box2d.destroyBody(body) end # Drawing the box def display # We look at each body and get its screen position pos = box2d.body_coord(body) # Get its angle of rotation a = body.getAngle rect_mode(Java::ProcessingCore::PConstants::CENTER) push_matrix translate(pos.x,pos.y) rotate(-a) fill(127) stroke(0) stroke_weight(2) rect(0,0,w,h) pop_matrix end end
Version data entries
4 entries across 4 versions & 1 rubygems