# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on November 25, 2009, 06:40:57 pm

 Title: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 25, 2009, 06:40:57 pm reference:http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=684.0Hi Ejs community, chance to give some tips how to implement when the blue object (centre is ox,oy) with height h and width w is hitting the lower tip of the inner jaws ( oxmin,oymin) for example that it will be blocked?my existing strategy adapted from http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1189 uses the condition (ox<=oxmin+w/2&&oy>oymin-h/2) { // check left boundary true to make blue object fit into the jaws  ox=oxmin+w/2;  which is necessary when it is inside the jaws say oy>oymin-h/2.but i can't figure out a way to make the boundary detection different from below and when inside the jaws because i am stuck cos both conditions i am using (ox<=oxmin+w/2&&oy>oymin-h/2) chance to help?Thanks!the picture for the context of understanding the codes below (http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1313.0;attach=1492;image)my object codes// to check boundary of blue object with the parts of the vernier caliperif(ox<=oxmin+w/2&&oy>oymin-h/2) { // check left boundary true ox=oxmin+w/2; if(x2+x<=ox+w/2&&oy>oymin-h/2){ // check object and jaws to stop motion&& object y above object y minimumx=w;}}if(x<0)x=0; // for the 2 jaws to meet at zeroelse if((ox>x2+x-w/2)&& (oy>oymin-h/2) &&  (oy < oymax-h/2) ){ // right jaw hit object and inside jaws   ox=x2+x-w/2; // to force the blue object into the open jaws}if(oy>oymax-h/2)oy=oymax-h/2; // check top boundary codes changes to suit lookang thinking Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 26, 2009, 04:08:18 pm after discussion with my friendsit seems that suggestion could beif( ox < oxmin && oy < oymin )oy=oymin-h/2; // check boundary codes of blue object top side hitting the left jawtrying not to nest the codes for ease of debugging.will try later at home :)need the feedback from the Ejs simulation to refine the codes further Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: Fu-Kwun Hwang on November 26, 2009, 04:10:53 pm To check if the blue object is hitting the inner jar located at (oxmin, oymin).1. check for y direction: it will only occurs if oy+h/2 >= oymin;2. check for x direction: ox-w/2 <= oxmin and ox+w/2>=oxmax  the above conditions can be combined with (ox-w/2-oxmin)*(ox+w/2-oxmax)<=0 The above two conditions need to be satisfied at the same with (with and operator; &&) Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 28, 2009, 08:57:32 pm after much testing and more testing and more testing,i realize the codes need to be inside the blue object on drag.i came up with these codes but it is still not ideal. the problem is it still runs the "detect bottom codes" when it is running the "inside codes". any help is greatly appreciated  ;DThis problem i am facing is due to my inability to code the conditions "detect bottom codes" differently as "inside codes". Any tips? I try to create knowledge by doing, but it is difficult without your help! :Dif (( ox-w/2-oxmin)*(ox+w/2-oxmax)>0&&(oy+h/2-oymin)*(oy+h/2-oymax)<0){ // "detect bottom codes" suggestion by wolfgang method by hwang to detect outside the jawsoy = oymin-h/2; // force blue object to be fixed just below jaw}if ((ox-w/2-oxmin)*(ox+w/2-oxmax)<=0 &&oy+h/2 >= oymin){ // "inside codes"  detect inside jaws  if ( (ox-w/2-oxmin)<=0) // if exceed into the left jaw  {    ox=oxmin+w/2; // force blue object to sit by the side of left jaw    }  if ((ox+w/2-oxmax)>=0) // if exceed into the right jaw  {    ox=oxmax+w/2; // force blue object to sit by the side of right jaw    }    } Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: Fu-Kwun Hwang on November 28, 2009, 11:21:05 pm add a variable out=true; as default (type= boolean)if(ox+w/2>oxmax)ox=oxmax-w/2;if(out){ if(ox-w/2oymin)oy=oymin; if(ox-w/2>=oxmin && ox+w/2<=oxmax && oy+h/2>oymin)out=false;}else{ if(ox-w/2oxmax)ox=oxmax-w/2; if(oy+h/2>oymax)oy=oymax-h/2;} Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 29, 2009, 09:37:06 am Thanks!i will continue to refine, i got some parts working now and some parts not working the way i wanted.need more time and determination to examine what every line of code is doing so as to improve it until i am satisfied.  the code that helped is. i am still working on it but for the rest of the people i think this is the code that " detection difference when touch from below and from inside the jaws?"// idea from http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1313.msg5065;topicseen#msg5065// need the -+20 dragging blue object to hard into jaws when blue object is drag to ensure outobject=false when inside if(ox-w/2 > oxmin-20 && ox+w/2 < oxmax+20 && oy+h/2 > oymin)outobject=false; else outobject = true;gotta balance family needs on sundays ;D Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: Fu-Kwun Hwang on November 29, 2009, 10:41:32 am If you use the above condition object inside/outside an area, you need another way to find out if user drag object into/out of that area.Some more code need to be added to my previous replied message:if(ox+w/2>oxmax)ox=oxmax-w/2;if(out){ if(ox-w/2oymin)oy=oymin; if(ox-w/2>=oxmin && ox+w/2<=oxmax && oy+h/2>oymin)out=false;// move into hot area}else{ if(ox-w/2oxmax)ox=oxmax-w/2; if(oy+h/2>oymax)oy=oymax-h/2; if(ox-w/2>=oxmin && ox+w/2<=oxmax && oy-h/2>oymin)out=true; // move out of hot area} Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 29, 2009, 05:13:43 pm Thanks prof hwang, ur ideas helped give me the tools to experiment with the codes until i got this. I must thank you for your support even on Saturday  and Sunday because u made a difference in my learning!this web2.0 forum is super cool, better than email because everyone else can learn, emails get lost in cyberspace after some time.The codes seems to work reasonable well most of the times for blue object on drag// use 2000 instead of 0 for realistic boundary detectionif (( ox-w/2-oxmin)*(ox+w/2-oxmax) > 2000 &&  (oy+h/2-oymin)*(oy+h/2-oymax) < 0 && outobject == true){ // "detect bottom codes" suggestion by wolfgang method by hwang to detect outside the jawsoy = oymin-h/2; // force blue object to be fixed just below jaw}if (x < w && outobject == true && (oy+h/2-oymin) > 0){ // jaw too narrow for objectoy = oymin-h/2; // force blue object to be fixed just below jaw}// use 2000 instead  of 0 for overcoming the blue object at the very topif ((ox-w/2-oxmin)*(ox+w/2-oxmax) <= 5000 && oy+h/2 - oymin >= 0 && outobject==false){ // "inside codes"  detect inside jaws  if ( (ox-w/2-oxmin)<= 0) // if exceed into the left jaw {    ox=oxmin+w/2; // force blue object to sit by the side of left jaw  }  if ((ox+w/2-oxmax) >= 0) // if exceed into the right jaw  {    ox=oxmax-w/2; // force blue object to sit by the side of right jaw    }    }http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=684. has the  new blue object codes, check it out ;) Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: Fu-Kwun Hwang on November 30, 2009, 08:46:44 am I download your code and modified it. I remove drag action in objectblue, disable objectnewdesign and add objectnewdesignFKH.Code:oxmax=oxmin+x;if(w>x){  if(oy>oymin-h/2)oy=oymin-h/2;  outobject=true;}else{// object is smaller than the width if(outobject){// if object is outside do the following check   if(ox-w/2>oxmin && ox+w/2oymin){// move inside     outobject=false;   }else{     if(ox-w/2oxmax){       if(oy+h/2>oymin)oy=oymin-h/2;     }   } }else{// if object is inside do the following check   if(oy+h/2oymax)oy=oymax-h/2;   if(ox-w/2oxmax)ox=oxmax-w/2;  } }  }The result is the following Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 30, 2009, 01:32:16 pm this is excellent prof hwang.i think i will keep your codes instead of using my codes.yours is more refined, mine got some problems when the mouse is moving all over screen LOL.thanks!, your r the best. hahahahabtw, there is a bug still in your codes, when the blue object is inside the jaws, on closing the jaws, when the xeasily fixed, i try to edit them now? Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: Fu-Kwun Hwang on November 30, 2009, 08:28:54 pm Move the above code to "drag action in objectblue" should solve the problem. Title: Re: how to have detection difference when touch from below and from inside the jaws? Post by: lookang on November 30, 2009, 10:39:44 pm thanks!just for ur infoi copied into the on drag action of the blue object the codes from objectnewdesignFKH, the same thing the blue object pops down when the jaws are meeting the blue object.so what i did was i disabled objectnewdesignFKHand enabled back the working codes from objectnewdesign....and the bug is gone!i think the codes u made in  objectnewdesignFKH need to be inside the blue object while the codes.....................else if((ox>x2+x-w/2)&&(oy>oymin-h/2)&&(oy<=oymax-h/2)){ // right jaw hit object and inside jaws   ox=x2+x-w/2; // to force the blue object into the open jaws}solved the bug.the new applet will be hosted there http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=684.msg2452#msg2452thanks for taking the your applet to the next level for usability in improving learning ;D.