CollideCheck.hx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. class CollideCheck extends hxd.App {
  2. static var RW = 100;
  3. static var RH = 30;
  4. var rrect : h2d.Graphics;
  5. var line : h2d.Graphics;
  6. override function init() {
  7. var size = RW - RH;
  8. var k = 10;
  9. rrect = new h2d.Graphics(s2d);
  10. rrect.beginFill(0xFFFFFFFF);
  11. for( i in 0...k+1 ) {
  12. var a = Math.PI * i / k - Math.PI / 2;
  13. rrect.lineTo(size + RH * Math.cos(a), RH * Math.sin(a));
  14. }
  15. for( i in 0...k+1 ) {
  16. var a = Math.PI * i / k + Math.PI / 2;
  17. rrect.lineTo(-size + RH * Math.cos(a), RH * Math.sin(a));
  18. }
  19. rrect.endFill();
  20. rrect.x = s2d.width >> 1;
  21. rrect.y = s2d.height >> 1;
  22. rrect.rotation = Math.PI / 3;
  23. line = new h2d.Graphics(s2d);
  24. line.beginFill(0xFFFFFFFF);
  25. line.drawRect(0, -0.5, 100, 1);
  26. line.endFill();
  27. //var r = new h2d.col.RoundRect(rrect.x, rrect.y, RW, RH, rrect.rotation);
  28. //mapCol( function(pt) return r.distance(pt) );
  29. }
  30. function mapCol( dist : h2d.col.Point -> Float, scale = 1. ) {
  31. var pt = new h2d.col.Point();
  32. var bmp = hxd.Pixels.alloc(s2d.width, s2d.height, BGRA);
  33. for( x in 0...bmp.width )
  34. for( y in 0...bmp.height ) {
  35. pt.x = x + 0.5;
  36. pt.y = y + 0.5;
  37. var d = dist(pt);
  38. if( d < 0 ) {
  39. var c = Std.int( -d * scale * 4 + 0x20 );
  40. if( c > 0xFF ) c = 0xFF;
  41. bmp.setPixel(x, y, 0xFF000000 | (c << 16));
  42. } else {
  43. var c = Std.int( d * scale );
  44. if( c > 0xFF ) c = 0xFF;
  45. bmp.setPixel(x, y, 0xFF000000 | c);
  46. }
  47. }
  48. var view = new h2d.Bitmap(h2d.Tile.fromPixels(bmp));
  49. view.alpha = 0.5;
  50. s2d.add(view, 0);
  51. bmp.dispose();
  52. }
  53. override function update(dt:Float) {
  54. var px = s2d.mouseX;
  55. var py = s2d.mouseY;
  56. var r = new h2d.col.RoundRect(rrect.x, rrect.y, RW * 2, RH * 2, rrect.rotation);
  57. var pt = new h2d.col.Point(px, py);
  58. rrect.rotation += 0.002;
  59. rrect.color.set(0, 0, 1);
  60. line.x = px;
  61. line.y = py;
  62. var n = r.getNormalAt(pt);
  63. line.rotation = Math.atan2(n.y, n.x);
  64. if( r.inside(pt) )
  65. rrect.color.set(0, 1, 0);
  66. }
  67. static function main() {
  68. new CollideCheck();
  69. }
  70. }