sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    SP Waves

    Scheduled Pinned Locked Moved SketchyPhysics
    18 Posts 5 Posters 9.9k Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F Offline
      faust07
      last edited by

      This is a script to export every frame directly within the run of a SketchyPhysics simulation. So emitters or Antons water waves can be exported correct (without a screen recorder). Put the code into the scripted feeld of one object (group) in the model. The simulation should be tested before, the export slows it dramatic down. The directory for the image files must be created before too. Instead of "d:/SPh_Temp/" You can create a directory of your choise. In the script, the directory and the image parameters can be changed.
      Sorry, do not know how to post code correctly. The middle two lines should be one in the script:

      ontick{
      Sketchup.active_model.active_view.write_image("d:/SPh_Temp/frame_%06d.jpg" % frame,1920,1080,true,0.8)
      }

      Run the simulation and you should find every frame as jpg file in the directory.

      example Video:
      http://youtu.be/uzp5MvY4QQI

      1 Reply Last reply Reply Quote 0
      • F Offline
        faust07
        last edited by

        increase wave travel distance and reflection on objects would be great... πŸ˜„ πŸ‘

        1 Reply Last reply Reply Quote 0
        • F Offline
          faust07
          last edited by

          Hi Anton. Here's a quick question, I have reduced the water surface to an almost realistic size. The bowl is about 1 m in diameter. How can I change amplitude, wavelength and speed of the waves so that it fits again. Thanks in advance!
          Yet another problem: "drops" of about 2.5 cm in diameter do not work in the normal setting worldscale 9.0 as an emitter. Here I have to reduce the worldscale to 7.0. Is this normal?


          wave test

          1 Reply Last reply Reply Quote 0
          • F Offline
            faust07
            last edited by

            Hi, Anton. A small request: Is there a way to change your script so that other non grouped entities (lines, curves, faces) in the model are not influenced? One possibility might be to put the geometry of the wave surface on a specific layer and to drive only these. When attempting camera paths (curves, polylines) to use in the wave model, these are extremely manipulated and broken after the start. Thanks in advance!

            1 Reply Last reply Reply Quote 0
            • A Offline
              Anton_S
              last edited by

              Hello fause07,

              Nice animation there.

              Tweaking wave parameters is possible and easy. Here is what the add_radial_wave function looks like:
              ` # Create wave.

              @param [Geom::Point3d] origin Wave position in global space.

              @param [Numeric] wavelength Wavelength in inches.

              @param [Numeric] amplitude Initial wave height.

              @param [Numeric] num_waves Number of waves to create.

              @param [Numeric] max_radius Maximum wave travel distance in inches.

              @param [Numeric] speed Speed in inches per second.

              def add_radial_wave(origin, wavelength, amplitude, num_waves = 5, max_radius = 100, speed = 1)
              ...
              end`
              To change wave properties, unhide and select the touch plane, open SP UI, and in the "scripted" field you should see this piece of code:

              onTouch { |toucher, speed, pos|
                wave = getVar('wave')
                wave.add_radial_wave(pos, speed*0.4, speed*0.6, 2, speed*10, 10)
              }
              

              Tweak the parameters of the add_radial_wave until the waves are fit for your surrounding.


              Bodies with a very small mass are considered static. Changing world scale is normal. Perhaps world scale feature was added exactly for that purpose.


              And it is possible to have only particular geometry be considered as waves. I tweaked the code to have all the geometry within the "SP Wave" layer be considered as waves. Geometry in other layers will not be affected. Select red cube and replace its code with this one:

              class Wave
                # @param [Sketchup;;Group] grp Wave plane.
                def initialize
                  @edges = Sketchup.active_model.entities.grep(Sketchup;;Edge)
                  @edges.uniq!
                  @vertices = []
                  wave_layer = Sketchup.active_model.layers["SP Wave"]
                  @edges.each { |e|
                    next if e.layer != wave_layer
                    @vertices.concat e.vertices
                  }
                  @vertices.uniq!
                  @waves = []
                  @ents = Array.new(@vertices.size)
                  @vectors = Array.new(@vertices.size)
                end
              
                # Create wave.
                # @param [Geom;;Point3d] origin Wave position in global space.
                # @param [Numeric] wavelength Wavelength in inches.
                # @param [Numeric] amplitude Initial wave height.
                # @param [Numeric] num_waves Number of waves to create.
                # @param [Numeric] max_radius Maximum wave travel distance in inches.
                # @param [Numeric] speed Speed in inches per second.
                def add_radial_wave(origin, wavelength, amplitude, num_waves = 5, max_radius = 100, speed = 1)
                  @waves << {
                    ;origin       => Geom;;Point3d.new(origin.to_a),
                    ;wavelength   => wavelength.to_f,
                    ;amplitude    => amplitude.to_f,
                    ;num_waves    => num_waves.to_f,
                    ;max_radius   => max_radius.to_f,
                    ;speed        => speed.to_f,
                    ;dist         => 0,
                    ;life         => Math;;PI*2*wavelength.to_f*num_waves.to_f
                  }
                end
              
                # Update all waves.
                def update
                  count = 0
                  @waves.delete_if { |w|
                    w[;dist] += w[;speed]
                    (w[;dist] - w[;max_radius]) > w[;life]
                  }
                  for i in 0...@vertices.size
                    pt = @vertices[i].position
                    z = -pt.z
                    @waves.each { |w|
                      cur_dist = Math.sqrt((pt.x - w[;origin].x)**2 + (pt.y - w[;origin].y)**2)
                      next if cur_dist > w[;dist] || cur_dist > w[;max_radius] || (w[;dist]-cur_dist) > w[;life]
                      z -= w[;amplitude] * Math.sin((w[;dist]-cur_dist)/w[;wavelength]) * (w[;max_radius]-cur_dist) / w[;max_radius] * (w[;life] + cur_dist - w[;dist]) / w[;life]
                    }
                    next if z.zero?
                    @ents[count] = @vertices[i]
                    @vectors[count] = Geom;;Vector3d.new(0, 0, z)
                    count += 1
                  end
                  Sketchup.active_model.entities.transform_by_vectors(@ents[0,count], @vectors[0,count])
                  #@edges.each { |e|
                  #  e.soft = true unless e.soft?
                  #  e.smooth = true unless e.smooth?
                  #}
                end
              
                # Reset water height.
                def reset
                  vectors = Array.new(@vertices.size)
                  for i in 0...@vertices.size
                    vectors[i] = Geom;;Vector3d.new(0, 0, -@vertices[i].position.z)
                  end
                  Sketchup.active_model.entities.transform_by_vectors(@vertices, vectors)
                end
              
              end # class Wave
              
              onStart {
                Sketchup.break_edges = false
                @wave = Wave.new
                @plane = [Geom;;Point3d.new(0,0,0), Geom;;Vector3d.new(0,0,1)]
                setVar('wave', @wave)
              }
              
              onUpdate {
                if key('v') == 1 && frame % 2 == 0
                  x,y = sp_tool_instance.cursorPos
                  line = Sketchup.active_model.active_view.pickray(x,y)
                  pt = Geom.intersect_line_plane(line, @plane)
                  @wave.add_radial_wave(pt, 4+rand(10), 5+rand(10), 2, 200, 10) if pt
                end
                @wave.update
              }
              
              onEnd {
                Sketchup.break_edges = true
                @wave.reset
              }
              

              Then create a new layer with a name "SP Wave" and place all the wave geometry in it.

              1 Reply Last reply Reply Quote 0
              • F Offline
                faust07
                last edited by

                Many thanks for the quick response! Meanwhile I had already found the small screws to adjust the parameters. The problem of the influence of other free geometry during physics simulation remains. Here the intermediate state of my Easter physics experimental scene:


                Easter wave test


                Frame 45 wave test

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Anton_S
                  last edited by

                  @faust07 said:

                  The problem of the influence of other free geometry during physics simulation remains.
                  Did you replace the code in red box with the new code provided in my prior post?

                  1 Reply Last reply Reply Quote 0
                  • F Offline
                    faust07
                    last edited by

                    Your new script runs great! Haven't seen your full post this morning. Don't know why. Thank you so much! Now I can implement camera paths or walking lines for moving objects without conflicts. πŸ‘

                    1 Reply Last reply Reply Quote 0
                    • F Offline
                      faust07
                      last edited by

                      Here some later happy Easter greetings to the friends of SketchyPhysics. Have a little too much tested and tasted the "Easter water"... Also a thank you to Anton for the important improvements to the plugin. One can already produce a lot of funny things with it without too much bugsplats. The video is a salute to my 2 week old granddaughter Matilda. β˜€


                      Matilda Welcome Easter Greetings

                      1 Reply Last reply Reply Quote 0
                      • A Offline
                        Anton_S
                        last edited by

                        Nicely done faust. You have a talent πŸ™‚

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement