/* ----------------------------------------------------------------------------- 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 isAnInvalidBody(p, body)= let p -> [object1 object2 iobj1mode iobj2mode state isposevent] in let SO3BodyGetMassMatrix body -> [mass _] in let SO3PhysicsGetMaterialName (SO3BodyGetMaterial body) -> bmat in if ((!strcmp bmat "ignore") || (SO3BodyGetIgnoreCollision body) || (body == (SO3SceneNodeGetBody object1)) || (body == (SO3SceneNodeGetBody object2)) || (SO3BodyGetFluid body)) && (!(SO3BodyGetType body) & 2) then ( 1; ) else ( 0; );; fun getValidRay(lray, p, cbtest)= 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 (exec cbtest with [p 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 state isposevent] 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 getValidRay (SO3PhysicsRayCastExt (V3DgetSession c3dXsession) src (subVectorF dest src) dist) p @isAnInvalidBody -> [body cdist normal] in ( SendPluginEvent inst "Distance" ftoa (if (cdist == nil) then dist else cdist) nil; if (body != nil) && (!state) then ( mutate p <- [_ _ _ _ 1 _]; SendPluginEvent inst "In" ftoa cdist nil; cbPlugGeneric 16 SO3ObjectGetName (SO3BodyGetSceneNode body) ftoa cdist; ) else if (body == nil) && (state) then ( mutate p <- [_ _ _ _ 0 _]; SendPluginEvent inst "Out" nil nil; cbPlugGeneric 17 SO3ObjectGetName (SO3BodyGetSceneNode body) ftoa cdist; ) else if ((body != nil) && state && isposevent) then ( let addVectorF src (multiplyVectorF normalizeVectorF (subVectorF dest src) [cdist cdist cdist]) -> [x y z] in SendPluginEvent inst "Position" strcatnSep (ftoa x)::(ftoa y)::(ftoa z)::nil " " nil; ) else nil; ); 0;; fun cbEnable(inst, from, action, param, rep, p)= mutate p <- [_ _ _ _ 0 _]; setPluginInstanceCbPreRender inst mkfun3 @cbGetRay p; 0;; fun cbDisable(inst, from, action, param, rep, p)= mutate p <- [_ _ _ _ 0 _]; setPluginInstanceCbPreRender inst nil; 0;; fun newOb(inst)= let (getPluginInstanceParam inst "object") -> objname1 in let (getPluginInstanceParam inst "object2") -> objname2 in // for compatibility let if ((atoi (getPluginInstanceParam inst "iscamera")) == 1) then "Current camera" else objname2 -> objname2 in let atoi (getPluginInstanceParam inst "init") -> init in let if init == nil then 1 else init -> init in let V3DgetObjectByName c3dXsession objname1 -> object1 in let V3DgetObjectTypeByName objname1 -> iobj1mode in let V3DgetObjectByName c3dXsession objname2 -> object2 in let V3DgetObjectTypeByName objname2 -> iobj2mode in let (IsInEditor inst) || IsEventLinked inst "Position" -> isposevent in let [object1 object2 iobj1mode iobj2mode 0 isposevent] -> p in ( if !init then nil else setPluginInstanceCbPreRender inst mkfun3 @cbGetRay p; PluginRegisterAction inst "Enable" mkfun6 @cbEnable p; PluginRegisterAction inst "Disable" mkfun6 @cbDisable p; setPluginInstanceCbDel inst @deleteOb; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;