Skip to content

Commit 2cfb32d

Browse files
committed
fix accel
1 parent cb1c396 commit 2cfb32d

File tree

1 file changed

+63
-57
lines changed

1 file changed

+63
-57
lines changed
Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
1-
#
2-
# Acceleration with Vectors
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Acceleration with Vectors
35
# by Daniel Shiffman.
46
# PVector was used in the original (instead of Vec2D)
5-
#
7+
#
68
# Demonstration of the basics of motion with vector.
79
# 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
911
# towards the mouse)
1012
#
11-
# For more examples of simulating motion and physics with vectors, see
13+
# For more examples of simulating motion and physics with vectors, see
1214
# Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D
1315
#
16+
class AccelerationWithVectors < Propane::App
17+
# A Mover object
18+
attr_reader :mover
1419

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
1724

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
2232

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+
####
3036

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
3443

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
6273
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
6977
end
7078
end
7179

72-
def settings
73-
size 640, 360, P2D
74-
end
80+
AccelerationWithVectors.new

0 commit comments

Comments
 (0)