Sha256: 5d2e465e5010f615dc7b101cabb52487ad994f9f816c5fd3c71e2572a1a81ff6
Contents?: true
Size: 1.16 KB
Versions: 6
Compression:
Stored size: 1.16 KB
Contents
# # Bouncing Ball with Vectors # by Daniel Shiffman. # # Demonstration of using vectors to control motion of body # This example is not object-oriented # See AccelerationWithVectors for an example of how to simulate motion using vectors in an object # See library/vecmath for more ruby like Vec2D and Vec3D examples # attr_reader :location, # Location of shape :velocity, # Velocity of shape :gravity # Gravity acts at the shape's acceleration def setup size(640,360) smooth @location = PVector.new(100,100) @velocity = PVector.new(1.5,2.1) @gravity = PVector.new(0,0.2) end def draw background(0) # Add velocity to the location. location.add(velocity) # Add gravity to velocity velocity.add(gravity) # Bounce off edges if ((location.x > width) || (location.x < 0)) velocity.x = velocity.x * -1 end if (location.y > height) # We're reducing velocity ever so slightly # when it hits the bottom of the window velocity.y = velocity.y * -0.95 location.y = height end # Display circle at location vector stroke(255) stroke_weight(2) fill(127) ellipse(location.x,location.y,48,48) end
Version data entries
6 entries across 6 versions & 1 rubygems