|
1 | | -# |
2 | | -# Acceleration with Vectors |
| 1 | +#!/usr/bin/env jruby -v -W2 |
| 2 | +# frozen_string_literal: true |
| 3 | +require 'propane' |
| 4 | +# Acceleration with Vectors |
3 | 5 | # by Daniel Shiffman. |
4 | 6 | # PVector was used in the original (instead of Vec2D) |
5 | | -# |
| 7 | +# |
6 | 8 | # Demonstration of the basics of motion with vector. |
7 | 9 | # A "Mover" object stores location, velocity, and acceleration as vectors |
8 | | -# The motion is controlled by affecting the acceleration (in this case |
| 10 | +# The motion is controlled by affecting the acceleration (in this case |
9 | 11 | # towards the mouse) |
10 | 12 | # |
11 | | -# For more examples of simulating motion and physics with vectors, see |
| 13 | +# For more examples of simulating motion and physics with vectors, see |
12 | 14 | # Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D |
13 | 15 | # |
| 16 | +class AccelerationWithVectors < Propane::App |
| 17 | + # A Mover object |
| 18 | + attr_reader :mover |
14 | 19 |
|
15 | | -# A Mover object |
16 | | -attr_reader :mover |
| 20 | + def setup |
| 21 | + sketch_title 'Acceleration With Vectors' |
| 22 | + @mover = Mover.new(width, height) |
| 23 | + end |
17 | 24 |
|
18 | | -def setup |
19 | | - sketch_title 'Acceleration With Vectors' |
20 | | - @mover = Mover.new(width, height) |
21 | | -end |
| 25 | + def draw |
| 26 | + background(0) |
| 27 | + # Update the location |
| 28 | + mover.update |
| 29 | + # Display the Mover |
| 30 | + mover.display |
| 31 | + end |
22 | 32 |
|
23 | | -def draw |
24 | | - background(0) |
25 | | - # Update the location |
26 | | - mover.update |
27 | | - # Display the Mover |
28 | | - mover.display |
29 | | -end |
| 33 | + #### |
| 34 | + # The Mover class |
| 35 | + #### |
30 | 36 |
|
31 | | -#### |
32 | | -# The Mover class |
33 | | -#### |
| 37 | + class Mover |
| 38 | + include Propane::Proxy |
| 39 | + # The Mover tracks location, velocity, and acceleration |
| 40 | + attr_reader :location, :velocity, :acceleration, |
| 41 | + # The Mover's maximum speed |
| 42 | + :topspeed |
34 | 43 |
|
35 | | -class Mover |
36 | | - # The Mover tracks location, velocity, and acceleration |
37 | | - attr_reader :location, :velocity, :acceleration, |
38 | | - # The Mover's maximum speed |
39 | | - :topspeed |
40 | | - |
41 | | - def initialize(width, height) |
42 | | - # Start in the center |
43 | | - @location = Vec2D.new(width / 2, height / 2) |
44 | | - @velocity = Vec2D.new |
45 | | - @topspeed = 5 |
46 | | - end |
47 | | - |
48 | | - def update |
49 | | - # Compute a vector that points from location to mouse |
50 | | - mouse = Vec2D.new(mouse_x, mouse_y) |
51 | | - @acceleration = mouse - location |
52 | | - # Set magnitude of acceleration |
53 | | - acceleration.set_mag(0.2) |
54 | | - # Velocity changes according to acceleration vector |
55 | | - @velocity += acceleration |
56 | | - # Limit the velocity to topspeed, PVector has a limit function |
57 | | - # here we supply a block that evaluates to a boolean to set |
58 | | - # the velocit conditionally |
59 | | - velocity.set_mag(topspeed) { velocity.mag > topspeed } |
60 | | - # Location changes by velocity vector |
61 | | - @location += velocity |
| 44 | + def initialize(width, height) |
| 45 | + # Start in the center |
| 46 | + @location = Vec2D.new(width / 2, height / 2) |
| 47 | + @velocity = Vec2D.new |
| 48 | + @topspeed = 5 |
| 49 | + end |
| 50 | + |
| 51 | + def update |
| 52 | + # Compute a vector that points from location to mouse |
| 53 | + mouse = Vec2D.new(mouse_x, mouse_y) |
| 54 | + @acceleration = mouse - location |
| 55 | + # Set magnitude of acceleration |
| 56 | + acceleration.set_mag(0.2) |
| 57 | + # Velocity changes according to acceleration vector |
| 58 | + @velocity += acceleration |
| 59 | + # Limit the velocity to topspeed, PVector has a limit function |
| 60 | + # here we supply a block that evaluates to a boolean to set |
| 61 | + # the velocit conditionally |
| 62 | + velocity.set_mag(topspeed) { velocity.mag > topspeed } |
| 63 | + # Location changes by velocity vector |
| 64 | + @location += velocity |
| 65 | + end |
| 66 | + |
| 67 | + def display |
| 68 | + stroke(255) |
| 69 | + stroke_weight(2) |
| 70 | + fill(127) |
| 71 | + ellipse(location.x, location.y, 48, 48) |
| 72 | + end |
62 | 73 | end |
63 | | - |
64 | | - def display |
65 | | - stroke(255) |
66 | | - stroke_weight(2) |
67 | | - fill(127) |
68 | | - ellipse(location.x, location.y, 48, 48) |
| 74 | + |
| 75 | + def settings |
| 76 | + size 640, 360, P2D |
69 | 77 | end |
70 | 78 | end |
71 | 79 |
|
72 | | -def settings |
73 | | - size 640, 360, P2D |
74 | | -end |
| 80 | +AccelerationWithVectors.new |
0 commit comments