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