|
1 | | -require 'toxiclibs' |
| 1 | +#!/usr/bin/env jruby -w |
| 2 | +require 'picrate' |
2 | 3 | require 'geomerative' |
| 4 | +require 'toxiclibs' |
3 | 5 |
|
4 | | -attr_reader :font, :input, :physics |
| 6 | +class PhysicsType < Processing::App |
5 | 7 |
|
6 | | -def settings |
7 | | - size(1280, 720, P3D) |
8 | | - smooth |
9 | | -end |
| 8 | + attr_reader :font, :input, :physics |
10 | 9 |
|
11 | | -def setup |
12 | | - sketch_title 'Physics Type' |
13 | | - @input = 'Hello!' |
14 | | - RG.init(self) |
15 | | - fnt = RG.load_font(data_path('ReplicaBold.ttf')) # file name |
16 | | - RG.text_font(fnt, 330) # RFont object, size |
17 | | - @font = RG.get_text(input) # String to RShape |
18 | | - RG.set_polygonizer(RCommand::UNIFORMLENGTH) |
19 | | - RG.set_polygonizer_length(10) # length of segment |
20 | | - init_physics |
21 | | - fill(255) |
22 | | -end |
| 10 | + def settings |
| 11 | + size(1280, 720, P3D) |
| 12 | + smooth |
| 13 | + end |
23 | 14 |
|
24 | | -def draw |
25 | | - physics.update |
26 | | - background(0) |
27 | | - stroke(255) |
28 | | - physics.springs.each do |s| |
29 | | - line(s.a.x, s.a.y, s.b.x, s.b.y) |
| 15 | + def setup |
| 16 | + sketch_title 'Physics Type' |
| 17 | + @input = 'Hello!' |
| 18 | + RG.init(self) |
| 19 | + fnt = RG.load_font(data_path('ReplicaBold.ttf')) # file name |
| 20 | + RG.text_font(fnt, 330) # RFont object, size |
| 21 | + @font = RG.get_text(input) # String to RShape |
| 22 | + RG.set_polygonizer(RCommand::UNIFORMLENGTH) |
| 23 | + RG.set_polygonizer_length(10) # length of segment |
| 24 | + init_physics |
| 25 | + fill(255) |
30 | 26 | end |
31 | | - physics.particles.each do |p| |
32 | | - ellipse(p.x, p.y, 3, 3) |
| 27 | + |
| 28 | + def draw |
| 29 | + physics.update |
| 30 | + background(0) |
| 31 | + stroke(255) |
| 32 | + physics.springs.each do |s| |
| 33 | + line(s.a.x, s.a.y, s.b.x, s.b.y) |
| 34 | + end |
| 35 | + physics.particles.each do |p| |
| 36 | + ellipse(p.x, p.y, 3, 3) |
| 37 | + end |
33 | 38 | end |
34 | | -end |
35 | 39 |
|
36 | | -def init_physics |
37 | | - @physics = Physics::VerletPhysics2D.new |
38 | | - # set screen bounds as bounds for physics sim |
39 | | - physics.set_world_bounds(Toxi::Rect.new(0, 0, width, height)) |
40 | | - # add gravity along positive Y axis |
41 | | - physics.add_behavior(Physics::GravityBehavior2D.new(TVec2D.new(0, 0.1))) |
42 | | - # multidimensional array of x and y coordinates |
43 | | - paths = font.get_points_in_paths |
44 | | - offset = TVec2D.new(200, 250) |
45 | | - return if paths.nil? |
46 | | - paths.length.times do |ii| |
47 | | - points = paths[ii] |
48 | | - path_particles = [] |
49 | | - points.length.times do |i| |
50 | | - p = Physics::VerletParticle2D.new( |
51 | | - points[i].x + offset.x, |
52 | | - points[i].y + offset.y |
53 | | - ) |
54 | | - physics.addParticle(p) |
55 | | - path_particles << p |
56 | | - physics.addSpring( |
57 | | - Physics::VerletSpring2D.new( |
58 | | - path_particles[i - 1], |
59 | | - p, |
60 | | - path_particles[i - 1].distanceTo(p), |
61 | | - 1 |
| 40 | + def init_physics |
| 41 | + @physics = Physics::VerletPhysics2D.new |
| 42 | + # set screen bounds as bounds for physics sim |
| 43 | + physics.set_world_bounds(Toxi::Rect.new(0, 0, width, height)) |
| 44 | + # add gravity along positive Y axis |
| 45 | + physics.add_behavior(Physics::GravityBehavior2D.new(TVec2D.new(0, 0.1))) |
| 46 | + # multidimensional array of x and y coordinates |
| 47 | + paths = font.get_points_in_paths |
| 48 | + offset = TVec2D.new(200, 250) |
| 49 | + return if paths.nil? |
| 50 | + paths.length.times do |ii| |
| 51 | + points = paths[ii] |
| 52 | + path_particles = [] |
| 53 | + points.length.times do |i| |
| 54 | + p = Physics::VerletParticle2D.new( |
| 55 | + points[i].x + offset.x, |
| 56 | + points[i].y + offset.y |
62 | 57 | ) |
63 | | - ) if i > 0 |
| 58 | + physics.addParticle(p) |
| 59 | + path_particles << p |
| 60 | + physics.addSpring( |
| 61 | + Physics::VerletSpring2D.new( |
| 62 | + path_particles[i - 1], |
| 63 | + p, |
| 64 | + path_particles[i - 1].distanceTo(p), |
| 65 | + 1 |
| 66 | + ) |
| 67 | + ) if i > 0 |
| 68 | + end |
| 69 | + first = path_particles.first |
| 70 | + last = path_particles.last |
| 71 | + physics.add_spring( |
| 72 | + Physics::VerletSpring2D.new( |
| 73 | + first, |
| 74 | + last, |
| 75 | + first.distance_to(last), |
| 76 | + 1 |
| 77 | + ) |
| 78 | + ) |
| 79 | + first.lock |
| 80 | + end |
64 | 81 | end |
65 | | - first = path_particles.first |
66 | | - last = path_particles.last |
67 | | - physics.add_spring( |
68 | | - Physics::VerletSpring2D.new( |
69 | | - first, |
70 | | - last, |
71 | | - first.distance_to(last), |
72 | | - 1 |
73 | | - ) |
74 | | - ) |
75 | | - first.lock |
76 | 82 | end |
77 | | -end |
| 83 | + |
| 84 | + PhysicsType.new |
0 commit comments