CannonScript

This script should be placed inside of your cannon's root prim (the cannon turret). Along with it goes your cannonball prim (which includes its script and the scripted tracer prim) and the sound file that you want the cannon to make when fired. If you don't want a sound, you can remove or comment out the first line in the **fire** function. The cannon itself should be made of phantom prims so that the cannonball doesn't collide with the cannon itself.

Return to main cannon page

code format="lsl2" //Written by Jeff Mao //Distributed under a Creative Commons Attribution 3.0 license [CC-BY](http://creativecommons.org/licenses/by/3.0/) //If you have questions about this, please feel free to contact me at jmao@mac.com. //Please share alterations, improvements, lesson plans related to this simulation, etc. //v1.3.2 //Rez an object on touch, with relative position, rotation, and velocity all described in the rezzing prim's coordinate system. string object = "CannonBall"; // Name of object in inventory vector relativePosOffset = <0.0, 0.0, 0.0>; vector relativeVel = <0.0, 0.0, 10.0>; // Traveling in this prim's "forward" direction at 10 m/s rotation relativeRot = <0.0, 0.0, 0.0, 0.0>; integer startParam = 10;

integer listen_handle; vector xyz_angles; rotation rot_xyzq;

string message; string myRotmessage;

fire {       llPlaySound("artillery1",1.0); //sound file from http://www.partnersinrhyme.com/soundfx/warsounds.shtml vector myPos = llGetPos; rotation myRot = llGetRot;

vector rezPos = myPos+relativePosOffset*myRot; vector rezVel = relativeVel*myRot; rotation rezRot = relativeRot*myRot;

llRezAtRoot(object, rezPos, rezVel, rezRot, startParam); llListenRemove(listen_handle); }

aim {    llSay(0,(string)message);

//rotate Cannon vector xyz_angles = (vector)message; vector angles_in_radians = xyz_angles *DEG_TO_RAD; rot_xyzq = llEuler2Rot(angles_in_radians); llSetRot(rot_xyzq); }

default {   state_entry {       llSay(0,"Cannon Ready. Touch the rings to control and fire."); }   link_message(integer sender_num, integer num, string str, key id) {       if (str=="aim") {            vector myPos = llGetPos; rotation myRot = llGetRot; vector myRot2 = llRot2Euler(myRot); vector myRot3 = myRot2 *RAD_TO_DEG;

llSay(0,"Current rotationVector= "+(string)myRot3);

listen_handle = llListen(5, "", llGetOwner, ""); llSay(0,"Please chat trajectory vector  on channel /5"); }       if (str=="velocity") {            llSay(0,"Current relativeVel= " + (string)relativeVel.z); listen_handle = llListen(9, "", llGetOwner, ""); llSay(0,"Please chat initial velocity on channel /9"); }       if (str=="fire") fire;

}   touch_start (integer num_detected) {       vector myPos = llGetPos; rotation myRot = llGetRot; vector myRot2 = llRot2Euler(myRot); vector myRot3 = myRot2 *RAD_TO_DEG;

llSay(0,"myPos= "+(string)myPos); llSay(0,"relativeVel= " + (string)relativeVel.z); llSay(0,"rotationVector= "+(string)myRot3); }

listen( integer channel, string name, key id, string message ) {       if (channel==5) {               llSay(0,"vector: "+(string)message);

//rotate Cannon vector xyz_angles = (vector)message; vector angles_in_radians = xyz_angles *DEG_TO_RAD; rot_xyzq = llEuler2Rot(angles_in_radians); llSetRot(rot_xyzq); // Stop listening until script is reset llListenRemove(listen_handle); string myRotmessage = (string)message; }       if (channel==9) {               llSay(0,"velocity: "+(string)message); relativeVel = <0.0, 0.0, (float)message>; // Stop listening until script is reset llListenRemove(listen_handle);

}

}

} code