From e7da74e9743f449d7d66cfac1f66fa110b2a38cd Mon Sep 17 00:00:00 2001 From: Ben Cragg Date: Wed, 10 Apr 2019 11:26:34 +0100 Subject: [PATCH 1/5] Added line collisions example --- community/line-collisions/main.go | 99 +++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 community/line-collisions/main.go diff --git a/community/line-collisions/main.go b/community/line-collisions/main.go new file mode 100644 index 0000000..46b6fbf --- /dev/null +++ b/community/line-collisions/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "image/color" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/imdraw" + "github.com/faiface/pixel/pixelgl" +) + +const ( + modeR = iota + modeL +) + +const ( + clickLineA = iota + clickLineB +) + +var ( + winBounds = pixel.R(0, 0, 1024, 768) + + r = pixel.R(10, 10, 70, 50) + l = pixel.L(pixel.V(20, 20), pixel.V(100, 30)) + + mode = modeR + clickLine = clickLineA +) + +func run() { + cfg := pixelgl.WindowConfig{ + Title: "Line collision", + Bounds: winBounds, + VSync: true, + } + + win, err := pixelgl.NewWindow(cfg) + if err != nil { + panic(err) + } + + imd := imdraw.New(nil) + + for !win.Closed() { + if mode == modeR { + win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125}) + } else { + win.Clear(color.RGBA{R: 21, G: 55, B: 18, A: 125}) + } + imd.Clear() + + if win.JustPressed(pixelgl.KeyR) { + mode = modeR + } + if win.JustPressed(pixelgl.KeyL) { + mode = modeL + clickLine = clickLineA + } + + if win.JustPressed(pixelgl.MouseButtonLeft) { + if mode == modeR { + rectToMouse := r.Center().To(win.MousePosition()) + r = r.Moved(rectToMouse) + } + + if mode == modeL { + if clickLine == clickLineA { + l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1))) + clickLine = clickLineB + } else { + l = pixel.L(l.A, win.MousePosition()) + clickLine = clickLineA + } + } + } + + imd.Color = color.Black + imd.Push(r.Min, r.Max) + imd.Rectangle(3) + + imd.Color = color.RGBA{R: 10, G: 10, B: 250, A: 255} + imd.Push(l.A, l.B) + imd.Line(3) + + imd.Color = color.RGBA{R: 250, G: 10, B: 10, A: 255} + for _, i := range r.IntersectionPoints(l) { + imd.Push(i) + imd.Circle(4, 0) + } + + imd.Draw(win) + win.Update() + } +} + +func main() { + pixelgl.Run(run) +} From 65f91d5cea25f7ca536900a1b58e1e7466587f37 Mon Sep 17 00:00:00 2001 From: Ben Cragg Date: Wed, 10 Apr 2019 11:34:20 +0100 Subject: [PATCH 2/5] Better structure --- community/line-collisions/main.go | 52 +++++++++++++------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/community/line-collisions/main.go b/community/line-collisions/main.go index 46b6fbf..2cc0ce6 100644 --- a/community/line-collisions/main.go +++ b/community/line-collisions/main.go @@ -8,11 +8,7 @@ import ( "github.com/faiface/pixel/pixelgl" ) -const ( - modeR = iota - modeL -) - +// These hold the state of whether we're placing the first or second point of the line. const ( clickLineA = iota clickLineB @@ -24,7 +20,6 @@ var ( r = pixel.R(10, 10, 70, 50) l = pixel.L(pixel.V(20, 20), pixel.V(100, 30)) - mode = modeR clickLine = clickLineA ) @@ -43,47 +38,42 @@ func run() { imd := imdraw.New(nil) for !win.Closed() { - if mode == modeR { - win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125}) - } else { - win.Clear(color.RGBA{R: 21, G: 55, B: 18, A: 125}) - } + win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125}) imd.Clear() - if win.JustPressed(pixelgl.KeyR) { - mode = modeR - } - if win.JustPressed(pixelgl.KeyL) { - mode = modeL - clickLine = clickLineA - } - + // When mouse left-click, move the rectangle so its' center is at the mouse position if win.JustPressed(pixelgl.MouseButtonLeft) { - if mode == modeR { - rectToMouse := r.Center().To(win.MousePosition()) - r = r.Moved(rectToMouse) - } + rectToMouse := r.Center().To(win.MousePosition()) + r = r.Moved(rectToMouse) + } - if mode == modeL { - if clickLine == clickLineA { - l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1))) - clickLine = clickLineB - } else { - l = pixel.L(l.A, win.MousePosition()) - clickLine = clickLineA - } + // When mouse right-click, set either the beginning or end of the line. + if win.JustPressed(pixelgl.MouseButtonRight) { + if clickLine == clickLineA { + // Set the beginning of the line to the mouse position. + // To make it clearer to the user, set the end position 1 pixel (in each direction) away from the first + // point. + l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1))) + clickLine = clickLineB + } else { + // Set the end point of the line. + l = pixel.L(l.A, win.MousePosition()) + clickLine = clickLineA } } + // Draw the rectangle. imd.Color = color.Black imd.Push(r.Min, r.Max) imd.Rectangle(3) + // Draw the line. imd.Color = color.RGBA{R: 10, G: 10, B: 250, A: 255} imd.Push(l.A, l.B) imd.Line(3) imd.Color = color.RGBA{R: 250, G: 10, B: 10, A: 255} + // Draw any intersection points. for _, i := range r.IntersectionPoints(l) { imd.Push(i) imd.Circle(4, 0) From 6b052dfa37b37a672fec1f35f3ee7f7e3684460f Mon Sep 17 00:00:00 2001 From: Ben Cragg Date: Wed, 10 Apr 2019 11:34:31 +0100 Subject: [PATCH 3/5] Intial commit --- community/line-collisions/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 community/line-collisions/README.md diff --git a/community/line-collisions/README.md b/community/line-collisions/README.md new file mode 100644 index 0000000..248351c --- /dev/null +++ b/community/line-collisions/README.md @@ -0,0 +1,10 @@ +# Line Collisions +Line collisions is a basic example of how to detect line collisions with a rectangle. + +## Instructions +You can set the rectangle position using a left-click of the mouse. + +You can set the line position with two right-clicks of the mouse; the first right-click will set the beginning of the +line the second will set the end of the line. + +Any intersection points where the rectangle and line intersection will appear as red dots. From dc53166dd9f4ebf7639c0057aa0d0211896c25b3 Mon Sep 17 00:00:00 2001 From: Ben Cragg Date: Mon, 15 Apr 2019 11:15:14 +0100 Subject: [PATCH 4/5] Initial commit --- .../line-collisions/lineCollisionScreenshot.png | Bin 0 -> 2022 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 community/line-collisions/lineCollisionScreenshot.png diff --git a/community/line-collisions/lineCollisionScreenshot.png b/community/line-collisions/lineCollisionScreenshot.png new file mode 100644 index 0000000000000000000000000000000000000000..d6af3060e33caf0ad9a46fa871068a4da26578b3 GIT binary patch literal 2022 zcmdUwk5f}e9>6yQ9|2#KgjRW2f8;6Qz+d4drQo1I@*qu#)Db!Yt(fRb8jMIvC?FDj z2$zJ~u|BK9Gbp4^BYGU7Ksy<&q?cN94ykV@Edt&-B5j4LEo~WV8&7V}xj*7=XLfe> z`~B?h_xs)N?zd%2fm#@o5CZ@ZYF;I`0>EQoUo1eN?N}84P=^rpoG{_=B4Oh^o_&IYo8tkN$D7vM4`(Xkx;X zqmZ2c%Y~lYZ9kto%N5V2ycKsW2Hj@y_TR3!a^-%*&8PLtqFhJM|1on-e!KS8r?rDI zAo6Kp7=ZWlDc}@gU?+*t@eFT8-v8_rOChpqI?T&?w0hm{d8IEp7?kaFA?&hVR3ABF>t(9>8CRei8gVy+DPcH}nMka*X7OiQ1I?_FW?QuM0OqU2 zvenF(lLK-wD~uM(mM~*&4&c+09s&}VpJ&?c zOaZAN^h5qK4Y7E zY#7V3?EC$m`pQ3V{qk%=KQ=gc`N8cg<7%*B*Rc%$Z)L+m&((!x*S;OR`gCl#cDGjM^J<9`OY3_Y-uQ8Kd!)`LWs%GU!O5R)J{`5~HJ;+7 zJ2alu1A~}M%#}RSl(&WBJ8SWfD1>rLnDX`kjFU6dGzUHtvjqYnx00E5 zIzVDlGMI^V@Zm|5PLgoj{!>m|hLsx1C&HihU<@k&<_OZ(FJrAfr;xP(pvE(dO2 zX(G`y6AMgC0KK8$_d{@J1=62KAuAQx`J^#h$?sQE$afOJMKEl0IntFwA;t>iYbntL z_mDCy9mYBrG3?QDWc0m&_HW}TB&4{)H$oZ-C4bNKM~``#MAG81yRJ{%zrSm+R-Zk&_};^Vz|=E?cB7V#F}NdM+!x_{XxI8#eE?fAv2qp(QLO zt(o%v@`iW)$4{Q2y+{6=0EcQG4qab=EApi5?=#I^z%0hf3>C;436VMZn~P@_VGG0w zIw9^q+>8{zM%A5wDI~`I8m-)yOxj;5aq;epu_MsK4AOo!T4+gOCRV{$axNBIlo|vV zMOXoh&rA7K`e6$QDi6Ms-q4-2YRs(I;^Igm(!ZOE$BN+{;y&zO`?z`F?#v32wwwf z->sF)HZl6b5u9)WN+mzp}_0Ng^xz2AeGAed;DPc;R&weH5p zE$H^R1d}VkZCuFf+>ZM#S~<6X(F;b5oRrbWEBTK^m>vFM<2Zo^75sNwQ2(X0rWz-z z5+|6N0MUQ`hRA~wAW*tQSPpZ#HX<1!$J)- Lxdmj8Qg`Uz#E1w+ literal 0 HcmV?d00001 From bb7baf0c7c6796e7c2f7cdf791f4cb6a1c1f3333 Mon Sep 17 00:00:00 2001 From: Ben Cragg Date: Mon, 15 Apr 2019 11:17:10 +0100 Subject: [PATCH 5/5] Added screenshot --- community/line-collisions/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/community/line-collisions/README.md b/community/line-collisions/README.md index 248351c..399034d 100644 --- a/community/line-collisions/README.md +++ b/community/line-collisions/README.md @@ -1,6 +1,8 @@ # Line Collisions Line collisions is a basic example of how to detect line collisions with a rectangle. +![Screenshot](lineCollisionScreenshot.png) + ## Instructions You can set the rectangle position using a left-click of the mouse.