Skip to content

Commit cb1c396

Browse files
committed
vec2d examples converted
1 parent 2498c2b commit cb1c396

23 files changed

+1724
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The particle_system_pshape.rb needs to be run with the --nojruby flag.
2+
The smoke_particle_system.rb needs to be run with the --nojruby flag.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to rp5 executable, and or opts as required
3+
4+
SAMPLES_DIR = './'
5+
6+
desc 'run demo'
7+
task default: [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each{ |sample| run_sample sample }
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob('*.rb').each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|jruby #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
java_import 'monkstone.vecmath.AppRender'
5+
# Click on the box and drag it across the screen.
6+
class AaBbExample < Propane::App
7+
attr_reader :block, :block_locked, :over_block, :bounds
8+
BLOCK_WIDTH = 150
9+
10+
def setup
11+
sketch_title 'AaBb Example'
12+
# on_top
13+
@block = Block.new(
14+
center: Vec2D.new(width / 2, height / 2),
15+
size: Vec2D.new(BLOCK_WIDTH, BLOCK_WIDTH))
16+
@locked = false
17+
@over_block = false
18+
@bounds = AaBb.new(
19+
center: Vec2D.new(width / 2, height / 2),
20+
extent: Vec2D.new(width - BLOCK_WIDTH, height - BLOCK_WIDTH)
21+
)
22+
end
23+
24+
def draw
25+
background 0
26+
fill 153
27+
if block.over?(Vec2D.new(mouse_x, mouse_y))
28+
@over_block = true
29+
stroke 255
30+
fill 255 if block_locked?
31+
else
32+
@over_block = false
33+
stroke 153
34+
end
35+
# Draw the box as a shape
36+
begin_shape
37+
block.points_array.each { |vec| vec.to_vertex(renderer) }
38+
end_shape(CLOSE)
39+
end
40+
41+
def renderer
42+
@renderer ||= AppRender.new(self)
43+
end
44+
45+
def block_locked?
46+
block_locked
47+
end
48+
49+
def over_block?
50+
over_block
51+
end
52+
53+
def mouse_pressed
54+
if over_block?
55+
@block_locked = true
56+
fill 255
57+
else
58+
@block_locked = false
59+
end
60+
end
61+
62+
def mouse_dragged
63+
return unless block_locked?
64+
position = Vec2D.new(mouse_x, mouse_y)
65+
block.new_position(position) { bounds.contains? position }
66+
end
67+
68+
def mouse_released
69+
@block_locked = false
70+
end
71+
72+
def settings
73+
size 640, 360
74+
smooth 4
75+
end
76+
end
77+
AaBbExample.new
78+
79+
# Use class to contain block behaviour
80+
class Block
81+
attr_reader :aabb
82+
83+
def initialize(center:, size:)
84+
@aabb = AaBb.new(center: center, extent: size)
85+
end
86+
87+
def new_position(center, &block)
88+
@aabb.position(center.dup, &block)
89+
end
90+
91+
def over?(vec)
92+
aabb.contains? vec
93+
end
94+
95+
# use for shape
96+
def points_array
97+
a = aabb.center - aabb.extent * 0.5
98+
c = aabb.center + aabb.extent * 0.5
99+
b = Vec2D.new(c.x, a.y)
100+
d = Vec2D.new(a.x, c.y)
101+
[a, b, c, d]
102+
end
103+
104+
# use for rect
105+
def points
106+
[aabb.center, aabb.extent]
107+
end
108+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# Acceleration with Vectors
3+
# by Daniel Shiffman.
4+
# PVector was used in the original (instead of Vec2D)
5+
#
6+
# Demonstration of the basics of motion with vector.
7+
# A "Mover" object stores location, velocity, and acceleration as vectors
8+
# The motion is controlled by affecting the acceleration (in this case
9+
# towards the mouse)
10+
#
11+
# For more examples of simulating motion and physics with vectors, see
12+
# Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D
13+
#
14+
15+
# A Mover object
16+
attr_reader :mover
17+
18+
def setup
19+
sketch_title 'Acceleration With Vectors'
20+
@mover = Mover.new(width, height)
21+
end
22+
23+
def draw
24+
background(0)
25+
# Update the location
26+
mover.update
27+
# Display the Mover
28+
mover.display
29+
end
30+
31+
####
32+
# The Mover class
33+
####
34+
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
62+
end
63+
64+
def display
65+
stroke(255)
66+
stroke_weight(2)
67+
fill(127)
68+
ellipse(location.x, location.y, 48, 48)
69+
end
70+
end
71+
72+
def settings
73+
size 640, 360, P2D
74+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'propane'
2+
#
3+
# Bouncing Ball with Vectors
4+
# by Daniel Shiffman.
5+
# PVector was used in the original (instead of Vec2D)
6+
#
7+
# Demonstration of using vectors to control motion of body
8+
# This example is not object-oriented
9+
# See AccelerationWithVectors for an example of how to simulate motion using vectors in an object
10+
class BouncingBalls < Propane::App
11+
RADIUS = 24
12+
13+
attr_reader :loc, # Location of shape
14+
:velocity, # Velocity of shape
15+
:gravity # Gravity acts at the shape's acceleration
16+
17+
def setup
18+
sketch_title 'Bouncing Ball'
19+
@loc = Vec2D.new(100, 100)
20+
@velocity = Vec2D.new(1.5, 2.1)
21+
@gravity = Vec2D.new(0, 0.2)
22+
end
23+
24+
def draw
25+
background(0)
26+
# Add velocity to the location.
27+
@loc += velocity
28+
# Add gravity to velocity
29+
@velocity += gravity
30+
# Bounce off edges
31+
velocity.x *= -1 unless (RADIUS..width - RADIUS).include?(loc.x)
32+
if loc.y > (height - RADIUS)
33+
# We're reducing velocity ever so slightly
34+
# when it hits the bottom of the window
35+
velocity.y *= -0.95
36+
loc.y = height - RADIUS
37+
end
38+
# Display circle at location vector
39+
stroke(255)
40+
stroke_weight(2)
41+
fill(127)
42+
ellipse(loc.x, loc.y, RADIUS * 2, RADIUS * 2)
43+
end
44+
45+
def settings
46+
size(640, 360)
47+
smooth 4
48+
end
49+
end
50+
51+
BouncingBalls.new

0 commit comments

Comments
 (0)