Advertisement
ec1117

better movement

Apr 7th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1.  
  2. static final int SZ=600;
  3. static final int PAD=200;
  4. int rad=SZ/20;
  5. ArrayList<Rect> rect=new ArrayList<Rect>();
  6. ArrayList<Seg> seg=new ArrayList<Seg>();
  7. Point st;
  8.  
  9. boolean in(Point x){
  10. for(Rect y:rect){
  11. if(x.x>=y.l && x.x<=y.r && x.y>=y.u && x.y<=y.d){
  12. return true;
  13. }
  14. }
  15. //TODO: impl dist to seg
  16. return false;
  17. }
  18.  
  19. void setup(){
  20. size(600,600);
  21. background(255);
  22. int sepline=(int)random(SZ);
  23. while(sepline<PAD || sepline>SZ-PAD){
  24. sepline=(int)random(SZ);
  25. }
  26. int u=(int)random(SZ), d=(int)random(SZ);
  27. int u2=(int)random(SZ), d2=(int)random(SZ);
  28. rect.add(new Rect((int)random(sepline),(int)random(sepline),u,d));
  29. rect.add(new Rect((int)random(SZ-sepline)+sepline,(int)random(SZ-sepline)+sepline,u2,d2));
  30. while(true){//not perfect
  31. st=new Point((int)random(SZ), (int)random(SZ));
  32. if(!in(st))break;
  33. }
  34. for(Rect x:rect){
  35. seg.add(new Seg(new Point(x.l,x.u),new Point(x.r,x.u)));
  36. seg.add(new Seg(new Point(x.l,x.d),new Point(x.r,x.d)));
  37. seg.add(new Seg(new Point(x.l,x.u),new Point(x.l,x.d)));
  38. seg.add(new Seg(new Point(x.r,x.u),new Point(x.r,x.d)));
  39. }
  40. }
  41.  
  42. float cross(PVector a, PVector b){
  43. return a.x*b.y-a.y*b.x;
  44. }
  45.  
  46. boolean intersect(Point a, Point b, Point c, Point d){
  47. PVector B=new PVector(b.x-a.x,b.y-a.y);
  48. PVector C=new PVector(c.x-a.x,c.y-a.y);
  49. PVector D=new PVector(d.x-a.x,d.y-a.y);
  50. float c1=cross(B,C);
  51. float c2=cross(B,D);
  52. int cc1=(int)(c1/abs(c1));
  53. int cc2=(int)(c2/abs(c2));
  54. println(cc1+" "+cc2+" "+c1+" "+c2);
  55. if(cc1+cc2!=0){
  56. return false;
  57. }
  58. println("HI AF");
  59. PVector A=new PVector(a.x-c.x,a.y-c.y);
  60. B=new PVector(b.x-c.x,b.y-c.y);
  61. D=new PVector(d.x-c.x,d.y-c.y);
  62. c1=cross(D,A);
  63. c2=cross(D,B);
  64. cc1=(int)(c1/abs(c1));
  65. cc2=(int)(c2/abs(c2));
  66. if(cc1+cc2!=0){
  67. return false;
  68. }
  69. return true;
  70. }
  71.  
  72. void draw(){
  73. clear();
  74. background(255);
  75. move();
  76. circle(st.x,st.y,rad);
  77. for(Rect x:rect){
  78. rect(x.l,x.u,x.r-x.l,x.d-x.u);
  79. }
  80. //line(st.x,st.y,mouseX,mouseY);
  81. PVector dir=new PVector(mouseX-st.x,mouseY-st.y);
  82. PVector mag=new PVector(mouseX-st.x,mouseY-st.y);
  83. println(dir.heading());
  84. dir.normalize();
  85. mag.normalize();
  86. mag.mult(2*SZ);
  87. for(Seg x:seg){
  88. if(intersect(st,new Point(st.x+mag.x,st.y+mag.y),x.x,x.y)){
  89. float L=0,R=mag.mag();
  90. for(int i=0;i<30;i++){
  91. float M=(L+R)/2;
  92. PVector test=PVector.mult(dir,M);
  93. if(intersect(st,new Point(st.x+test.x,st.y+test.y),x.x,x.y)){
  94. R=M;
  95. } else L=M;
  96. }
  97. mag=PVector.mult(dir,L);
  98. }
  99. }
  100. line(st.x,st.y,st.x+mag.x,st.y+mag.y);
  101. }
  102.  
  103. final static float move=5;
  104. boolean isLeft,isRight,isDown,isUp;
  105.  
  106. void keyPressed(){//strafe and vel
  107. setMove(keyCode,true);
  108. //if(key==CODED){
  109. // if(keyCode==LEFT){
  110. // Point sy=new Point(st.x-move,st.y);
  111. // if(!in(sy))st=sy;
  112. // }
  113. // if(keyCode==UP){
  114. // Point sy=new Point(st.x,st.y-move);
  115. // if(!in(sy))st=sy;
  116. // }
  117. // if(keyCode==RIGHT){
  118. // Point sy=new Point(st.x+move,st.y);
  119. // if(!in(sy))st=sy;
  120. // }
  121. // if(keyCode==DOWN){
  122. // Point sy=new Point(st.x,st.y+move);
  123. // if(!in(sy))st=sy;
  124. // }
  125. //}
  126. }
  127.  
  128. void keyReleased(){
  129. setMove(keyCode,false);
  130. }
  131.  
  132. void move(){
  133. float spd=move;
  134. if((isRight||isLeft) && (isDown||isLeft)){
  135. spd/=1.41;
  136. }
  137. float x=constrain(st.x+spd*((int(isRight)-int(isLeft))),rad,SZ-rad);
  138. float y=constrain(st.y+spd*((int(isDown)-int(isUp))),rad,SZ-rad);
  139. println(x+" "+y+isRight+" "+isLeft+" "+isUp);
  140. if(!in(new Point(x,y))){
  141. st=new Point(x,y);
  142. } else if(!in(new Point(x,st.y))){
  143. st=new Point(x,st.y);
  144. } else if(!in(new Point(st.x,y))){
  145. st=new Point(st.x,y);
  146. }
  147. }
  148.  
  149. void setMove(final int code, boolean act){
  150. if(code==LEFT){
  151. isLeft=act;
  152. }
  153. if(code==UP){
  154. isUp=act;
  155. }
  156. if(code==DOWN){
  157. isDown=act;
  158. }
  159. if(code==RIGHT){
  160. isRight=act;
  161. }
  162. }
  163.  
  164. class Point{
  165. float x,y;
  166. Point(float a, float b){
  167. x=a;y=b;
  168. }
  169. }
  170.  
  171. class Seg{
  172. Point x, y;
  173. Seg(Point a, Point b){
  174. x=a;y=b;
  175. }
  176. }
  177.  
  178. class Rect{
  179. int l,r,u,d;
  180. Rect(int a, int b, int c, int e){
  181. if(c<=e){
  182. u=c;
  183. d=e;
  184. } else {
  185. u=e;
  186. d=c;
  187. }
  188. if(a<=b){
  189. l=a;
  190. r=b;
  191. } else {
  192. l=b;
  193. r=a;
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement