/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ fun getValidRay(lray)= let nil -> body in let 0.0 -> dist in let [0.0 1.0 0.0] -> normal in ( while ((lray != nil) && (body == nil)) do ( let hd lray -> [tbody tdist tnormal] in if (((SO3BodyGetType tbody) == 1) || (SO3BodyGetIgnoreCollision tbody) || (SO3BodyGetFluid tbody)) then nil else ( set body = tbody; set dist = tdist; set normal = tnormal; ); set lray = tl lray; ); [body dist normal] );; fun deleteOb(inst)= setPluginInstanceCbPreRender inst nil; 0;; fun cbGetRay(inst, viewstr, p)= let p -> [object1 object2 iobj1mode iobj2mode force distatt] in let if (iobj1mode != 0) then ( let V3DgetDefaultCamera c3dXsession -> curcamera in V3DgetCameraByType c3dXsession curcamera iobj1mode; ) else object1 -> object1 in let if (iobj2mode != 0) then ( let V3DgetDefaultCamera c3dXsession -> curcamera in V3DgetCameraByType c3dXsession curcamera iobj2mode; ) else object2 -> object2 in if (object1 == nil || object2 == nil) then nil else let SO3ObjectGetGlobalPosition object1 -> src in let SO3ObjectGetGlobalPosition object2 -> dest in let src -> [ox oy oz] in let dest -> [cx cy cz] in let sqrt ((sqr(cx -. ox)) +. (sqr(cy -. oy)) +. (sqr(cz -. oz))) -> dist in let maxf 0.1 dist -> dist in let normalizeVectorF (subVectorF dest src) -> dir in let getValidRay (SO3PhysicsRayCastExt (V3DgetSession c3dXsession) src dir dist) -> [rbody cdist rnormal] in let cdist /. dist -> distcoef in if (rbody == nil) then nil else ( let addVectorF src (multiplyVectorF (subVectorF dest src) [distcoef distcoef distcoef]) -> [fx fy fz] in let SO3BodyGetMassMatrix rbody -> [mass _] in let maxf 0.9 mass -> mass in let 1.0 -. (distcoef *. distatt) -> attenuation in let (force /. mass) *. attenuation -> force in let SO3PhysicsGetMaterialName (SO3BodyGetMaterial rbody) -> mat1 in let SO3ObjectGetName (SO3BodyGetSceneNode rbody) -> nodename in ( //addLogMessage strcatn "mass: "::(ftoa mass)::" dist: "::(ftoa dist)::" cdist: "::(ftoa cdist)::" coef: "::(ftoa distcoef)::" attenuation: "::(ftoa attenuation)::nil; //SO3WorldApplyImpulsePoint (V3DgetSession c3dXsession) [fx fy fz] 5.0 force; cbPlugGeneric 16 nodename ftoa cdist; // ray in SO3BodyAddImpulse rbody (multiplyVectorF dir [force force force]) [fx fy fz]; cbPlugGeneric 23 nodename ftoa cdist; // Hit SendPluginEvent inst "Hit distance" (ftoa cdist) nil; SendPluginEvent inst "Hit position" strcatn (ftoa fx)::" "::(ftoa fy)::" "::(ftoa fz)::nil nil; SendPluginEvent inst "Hit object" strcatn nodename::" "::mat1::nil nil; cbPlugGeneric 17 nodename ftoa cdist; // ray out ); ); setPluginInstanceCbPreRender inst nil; 0;; fun cbTrigger(inst, from, action, param, rep, p)= setPluginInstanceCbPreRender inst mkfun3 @cbGetRay p; 0;; fun cbSetForce(inst, from, action, param, rep, p)= if ((atof param) == nil) then nil else mutate p <- [_ _ _ _ (atof param) _]; 0;; fun cbSetAttenuation(inst, from, action, param, rep, p)= if ((atof param) == nil) then nil else mutate p <- [_ _ _ _ _ (atof param)]; 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object") -> objname1 in let (getPluginInstanceParam inst "object2") -> objname2 in let atof (getPluginInstanceParam inst "force") -> force in let if force == nil then 10.0 else force -> force in let atof (getPluginInstanceParam inst "distatt") -> distatt in let if distatt == nil then 0.1 else distatt -> distatt in let V3DgetObjectByName c3dXsession objname1 -> object1 in let V3DgetObjectTypeByName objname1 -> iobj1mode in let V3DgetObjectByName c3dXsession objname2 -> object2 in let V3DgetObjectTypeByName objname2 -> iobj2mode in let [object1 object2 iobj1mode iobj2mode force distatt] -> p in ( PluginRegisterAction inst "Trigger" mkfun6 @cbTrigger p; PluginRegisterAction inst "Set force" mkfun6 @cbSetForce p; PluginRegisterAction inst "Set attenuation" mkfun6 @cbSetAttenuation p; setPluginInstanceCbDel inst @deleteOb; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;