SketchyPhysicsWiki
Advertisement

Advanced examples[]

Variables[]

setVar("var",getVar("var")+1)

Increases a variable by one every time it runs.

Change the state of an object: Hide/Unhide[]

Put this piece of code in the 'OnTick' box:

if key('space')==1; $curEvalGroup.hidden=true;
else $curEvalGroup.hidden=false; end

or

if key('space')==1; this.hidden=true;
else this.hidden=false; end

Which means the object will be visible if space is pressed. To hide, simply change 1 to 0 or true to hidden and vice versa.

Ready-to-use scripts[]

SketchyPhysics 3.1 Advanced Methods (By Diego Silva) In the SP3.1 version, was added new resources to manipulate shapes:

  • teleport(pos,recurse=true) - Where pos is a Point3D location.
  • getTorque()
  • setTorque(torque)
  • getVelocity()
  • setVelocity(velocity)
  • setLinearDamping(damp)
  • setAngularDamping(damp)

Example of teleport: onclick{ teleport([99,349,10]) } The result is: when the user click in the body with this code, it will be teletransported to specified point.


The toggle script (By Wacov)[]

if frame==1;setVar("repeat",0);setVar("toggle",0);end;if joybutton("a")==0;setVar("repeat",1);getVar("toggle");elsif getVar("repeat")==0;getVar("toggle");elsif getVar("toggle")==0;setVar("repeat",0);getSetVar("toggle",1);else;setVar("repeat",0);getSetVar("toggle",0);end;

Versatile, good for many kinds of switches. Change 'joybutton("a")' to any key or button input if your model requires; the output of the script is the variable called "toggle."

Particle effects (Non LazyScript)[]

If you haven't downloaded LazyScript yet, don't worry. You still have a chance at your own particle effects. However, you will need this script to continue:


if frame==0 #onstart
Sketchup.active_model.start_operation "Particle Setup"
##SETUP---------------------------------------------------------------
   #create list and struct for particles
   $particleList=Array.new
   $cpntList=Array.new
   #default onupdate behavior
   $defaultSmoke=Proc.new{|p|
   scale=Geom::Transformation.scaling(p.group.bounds.center,p.scale)
   p.group.transform! scale
   p.velocity[0]=(p.velocity[0]*0.8)+p.rvals[0]/10
   p.velocity[1]=(p.velocity[1]*0.8)+p.rvals[1]/10
   p.velocity[2]=p.velocity[2]-(p.grav*3)
   speed=Geom::Transformation.new(p.velocity)
   p.group.transform! speed
   if p.type=="2D"
      camvec=Sketchup.active_model.active_view.camera.direction
      grpy=p.group.transformation.yaxis
      angle=grpy.angle_between camvec
      if angle>0.5
         vec=grpy.cross camvec
         t=Geom::Transformation.rotation p.group.bounds.center,vec,angle
         p.group.transform! t
      end
   end
   p.mat.alpha=p.mat.alpha-p.alpha/(p.life/2) 
}  
   #create template particles
   $particle3D=Sketchup.active_model.entities.add_group
   e = $particle3D.entities
   c1 = e.add_circle([0,0,0],[1,0,0],1,8) 
   c2 = e.add_circle([0,0,-10],[0,0,1],1,8) 
   c1.each{|edge|edge.hidden=true}
   c2.each{|edge|edge.hidden=true}
   f = e.add_face(c1)
   status = f.followme(c2) 
   c2.each{|edge|edge.erase!}
   $particle3D.hidden=true
   $particle2D=Sketchup.active_model.entities.add_group
   e = $particle2D.entities
   c1 = e.add_circle([0,0,0],[0,1,0],1,12)
   c1.each{|edge|edge.hidden=true}
   f = e.add_face(c1)
   $particle2D.hidden=true
#END SETUP-----------------------------------------------------------
   class Particle

attr_accessor(:group,:grav,:scale,:mat,:life,:velocity,:rvals,:start,:position,:onupdatefunc,:type,:alpha)

       def onupdate(&block) 
         self.onupdatefunc=block 
      end  
      def initialize(pos,radius,g,grav,scale,mat,alpha,life,velocity) 
         #particle var 
         p=self  
         #2D or 3D 
         if g==$particle2D 
            p.type="2D" 
         else 
            p.type="3D" 
         end 
        
         #material setup
         material=Sketchup.active_model.materials.add("FX")
         material.color=mat
         material.alpha=alpha
         p.mat=material
         #copy template group
         xform=Geom::Transformation.translation pos
         group=Sketchup.active_model.entities.add_instance(g.definition,xform)
         #transform copy to correct radius
         t=Geom::Transformation.scaling pos,radius
         group.transform! t
         #transform copy to face camera
         if p.type=="2D"
            camvec=Sketchup.active_model.active_view.camera.direction
            grpy=group.transformation.yaxis
            angle=grpy.angle_between camvec
            vec=grpy.cross camvec
            t=Geom::Transformation.rotation group.bounds.center,vec,angle
            group.transform! t
         end         

         #apply material to copy
         group.material=material
        
         #set vars for particle
         p.group=group
         p.grav=grav
         p.scale=scale+(rand/40)
         p.life=life
         p.alpha=alpha
         p.velocity=velocity
         p.rvals=[0.5-rand,0.5-rand]
         p.start=$parFrame
         return p
      end
   end
   
   ##METHOD---------------------------------------------------------------
   def createParticle(pos=$curEvalGroup.bounds.center,radius=nil,group=$particle3D,rate=5,velocity=[0,0,0],grav=-0.1,scale=1.01,mat="Black",alpha=0.6,life=50)
      if radius==nil && $parFrame%rate==1
         c=Sketchup.active_model.entities.add_cpoint pos
         $cpntList.push(c)
         return c
      elsif $parFrame%rate == 1 or rate == 1
         p=Particle.new(pos,radius,group,grav,scale,mat,alpha,life,velocity)
         $particleList.push(p)
         p.onupdatefunc=$defaultSmoke
         return p
      end #if
   end ##METHOD-----------------------------------------------------------
Sketchup.active_model.commit_operation
end #onstart
#ontick
   Sketchup.active_model.start_operation "Particle Update", true
   $parFrame=frame
   Sketchup.active_model.materials.purge_unused
   ##UPDATE---------------------------------------------------------------
   if frame%2==1
      $particleList.each{|p|
         unless p.onupdatefunc==nil
            p.onupdatefunc.call(p)
         end
         if frame>(p.start+p.life)
            p.group.erase!
            $particleList=$particleList-[p]
         end
      } #particleList.each
   end ##UPDATE-----------------------------------------------------------
   Sketchup.active_model.commit_operation
#END ontick
if key('end')==1
   if $particleList!=nil
      ##CLEANUP---------------------------------------------------------------
      $particleList.each{|p|unless p.group==nil
         p.group.erase!
         $particleList=$particleList-[p]
         end
      }
      $cpntList.each{|c|c.erase!}
      $particle3D.erase!
      $particle2D.erase!
      Sketchup.active_model.materials.purge_unused
      ##END CLEANUP-----------------------------------------------------------
   end
   Sketchup.send_action("selectSelectionTool:")
end

To use, just copy/paste into any OnTick box (preferably the floor). Here is a good starter:

####PARTICLE CONFIGURATION####
par_position = $curEvalGroup.bounds.center
par_radius   = 12
par_style    = "2D"
par_rate     = 5
par_velocity = [5,10,-1]
par_gravity  = -0.05
par_scale    = 1.01
par_color    = "Black"
par_alpha    = 1.0
par_lifetime = 75
####PARTICLE CREATION####
if par_style=="2D"
   group=$particle2D
elsif par_style=="3D"
   group=$particle3D
else
   group=par_style
end

createParticle par_position, par_radius, group, par_rate, par_velocity, par_gravity, par_scale, par_color, par_alpha, par_lifetime



Now that you have done that, you may RUN WILD! Try this one in the 'ontick box,' for example:


####PARTICLE CONFIGURATION#### 
par_position = $curEvalGroup.bounds.center 
par_radius   = 7 
par_style    = "3D" 
par_rate     = 1 
par_velocity = [0,0,0] 
par_gravity  = -0.05 
par_scale    = 1.02 
par_color    = "HotPink" 
par_alpha    = 0.4 
par_lifetime = 100 

####PARTICLE CREATION#### 
if par_style=="2D"
    group=$particle2D
elsif par_style=="3D"
   group=$particle3D
else
   group=par_style
end
createParticle par_position, par_radius, group, par_rate, par_velocity, par_gravity, par_scale, par_color, par_alpha, par_lifetime


You may be happy with your smoke. OH NO! You fail to press the end key on the keyboard. However, you may delete the leftovers. These also can be modified to suit your needs. Just change the text after par_(something), with the exception of par_position.


Advertisement