About Monkey 2 › Forums › Monkey 2 Projects › FIRST HTML5 PYRO FOR MONKEY2 DEMO EVER!
This topic contains 40 replies, has 10 voices, and was last updated by
Playniax
2 years, 6 months ago.
-
AuthorPosts
-
September 24, 2016 at 7:05 pm #4086
Sorry for belated answer about my poly checking function.
I wrote demo for demonstration. It contains Polygon class and some helpers.
Full code:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280Namespace polytest#Import "<std>"#Import "<mojo>"Using std..Using mojo..Function Main()New AppInstanceLocal wnd := New AppWindow("Polygon Test", 600, 600)Local game := New GameLogicwnd.OnRenderCallback += Lambda(canvas:Canvas)game.Draw(canvas)EndApp.Run()End' AppWindowClass AppWindow Extends WindowField OnRenderCallback:Void(canvas:Canvas)Method New( title:String,width:Int,height:Int,flags:WindowFlags=Null )Super.New( title,New Recti( 0,0,width,height ),flags|WindowFlags.Center )EndMethod OnRender(canvas:Canvas) OverrideApp.RequestRender()OnRenderCallback(canvas)EndEnd' GameLogicClass GameLogicMethod New()Local poly := New Polygon(New Float[](0,0, 150,50, 200,100, 100,150))poly.Origin = New Vec2f(100,50)polyObj = New PolyObjectpolyObj.poly = polypolyObj.pos = New Vec2f(300,300)EndMethod Update()Local p := Mouse.LocationLocal over := polyObj.PointIn(p.x, p.y)polyObj.color = over ? Color.Green Else Color.BluepolyObj.poly.Rotate(0.005)EndMethod Draw(canvas:Canvas)Update() 'hm, update in draw...polyObj.Draw(canvas)EndPrivateField polyObj:PolyObjectEnd' PolyObjectClass PolyObjectField poly:PolygonField pos:Vec2fField color := Color.WhiteMethod Draw(canvas:Canvas)If poly = Null ReturnLocal p := pos-poly.Origincanvas.Translate(p.x,p.y)canvas.Color = colorcanvas.DrawPoly(poly.Points)canvas.Color *= 0.25canvas.DrawRect(poly.Bounds)canvas.Color = Color.Blackcanvas.DrawOval(poly.Origin.x-2, poly.Origin.y-2, 4, 4)EndMethod PointIn:Bool(px:Float, py:Float)px -= pos.x-poly.Origin.x 'bad place to get poly.Originpy -= pos.y-poly.Origin.yReturn poly.PointIn(px, py)EndEnd' PolygonClass PolygonMethod New(xyArray:Float[])_points = New Float[xyArray.Length]For Local k := 0 Until xyArray.Length_points[k] = xyArray[k]NextUpdateBounds()EndMethod New(rect:Rectf)_points = New Float[8]_points[0] = rect.Left_points[1] = rect.Top_points[2] = rect.Right_points[3] = rect.Top_points[4] = rect.Right_points[5] = rect.Bottom_points[6] = rect.Left_points[7] = rect.Bottom_bounds = rectEndProperty Points:Float[]()Return _pointsEndProperty Bounds:Rectf()Return _boundsEndProperty Origin:Vec2f()Return _originSetter(value:Vec2f)_origin = valueEndProperty Rotation:Float()Return _rotationEndMethod Rotate(angle:Float)Local ox := _origin.xLocal oy := _origin.yLocal cos := Cos(angle)Local sin := Sin(angle)For Local k := 0 Until _points.Length Step 2Local px := _points[k]-oxLocal py := _points[k+1]-oy_points[k] = ox + px*cos - py*sin_points[k+1] = oy + px*sin + py*cosNext_rotation = (_rotation + angle) Mod 360.0UpdateBounds()EndMethod Move(dx:Float, dy:Float)For Local k := 0 Until _points.Length Step 2_points[k] += dx_points[k+1] += dyNextLocal v := New Vec2f(dx,dy)_bounds += v_origin += vEndMethod PointIn:Bool(point:Vec2f)If Not _bounds.Contains(point) Then Return FalseReturn PointIn(_points,point)EndMethod PointIn:Bool(px:Float, py:Float)Return PointIn(New Vec2f(px,py))EndFunction PointIn:Bool(xy:Float[], point:Vec2f)Local px := point.x, py := point.yLocal x1:Float, y1:Float, x2:Float, y2:FloatLocal koef:Float, koefA:FloatLocal cnt:Int = xy.LengthLocal sign:Int, sign2:IntFor Local k := 0 Until cnt Step 2x1 = xy[k]y1 = xy[k + 1]If k + 3 < cntx2 = xy[k + 2]y2 = xy[k + 3]Else 'connect with first pointx2 = xy[0]y2 = xy[1]Endifsign = Sgn(x2 - x1)sign2 = Sgn(px - x1)'prevent division by zeroIf sign = 0x2 += 0.0001sign = 1EndifIf sign2 = 0px += 0.0001sign2 = 1EndIf'the slope coefficient (y = k*x + b)koef = (y1 - y2) / (x2 - x1)koefA = (y1 - py) / (px - x1)'checkingIf (sign2 = sign And koefA > koef) Or (sign2 <> sign And koefA < koef)Return FalseEndifNextReturn TrueEnd FunctionPrivateField _points:Float[] 'xy pairs in one dimention arrayField _bounds:Rectf 'bounds used for fast collision detectionField _origin := New Vec2f(0,0)Field _rotation:FloatMethod UpdateBounds()Local minX := _points[0]Local maxX := _points[0]Local minY := _points[1]Local maxY := _points[1]For Local k := 2 Until _points.Length Step 2Local px := _points[k]Local py := _points[k+1]If px < minXminX = pxElseif px > maxXmaxX = pxEndifIf py < minYminY = pyElseif py > maxYmaxY = pyEndifNextIf _bounds = Null Then _bounds = New Rectf_bounds.Left = minX_bounds.Right = maxX_bounds.Top = minY_bounds.Bottom = maxYEndEndSeptember 25, 2016 at 6:54 am #4088@nerobot: here’s another PointInPoly() , not that it seems you need it
Monkey1234567891011121314151617181920Function IsInsidePoly:Int(polypoints:Float[],x:Float,y:Float)Local j:Int = polypoints.Length()-2Local oddNodes:IntFor Local i:Int =0 Until polypoints.Length() Step 2If (polypoints[i+1]< y And polypoints[j+1]>=y ) Or (polypoints[j+1]< y And polypoints[i+1]>=y)If (polypoints[i]<=x Or polypoints[j]<=x)If (polypoints[i]+(y-polypoints[i+1])/(polypoints[j+1]-polypoints[i+1])*(polypoints[j]-polypoints[i])<x)oddNodes = 1 ~ oddNodesEndifEndifEndifj=iNextReturn oddNodesEnd FunctionSeptember 27, 2016 at 9:59 am #4113Your formula looks like a magic for me.
And it’s offtopic here.:)
September 30, 2016 at 6:51 am #4170@playniax: Since this ( https://github.com/blitz-research/monkey2/issues/82 ) rendering bug looks somewhat similar to the one I reported on the monkey1 forum ( http://www.monkey-x.com/Community/posts.php?topic=12629&post=124043&view=all#124043 ) , I’d be happy to test spiral again, if you would like to recomplie it with MX 1.07
@Evgenly Goroshkin : As I remember it, it’s from http://alienryderflex.com/polygon/ , Nathan Mercer’s version
September 30, 2016 at 10:30 am #4172Monkey123456789101112131415161718192021222324252627282930' returns' 0 = no collision' 1 = line collion' 2 = point insideFunction PolyCollision:Int(polypoints:Float[],x:Float,y:Float)Local j:Int = polypoints.Length-2Local cn:Int = 0Local cn2:Int = 0For Local i:Int=0 Until polypoints.Length Step 2If i<=polypoints.Length-3 And (((polypoints[i+1] <= y) And (polypoints[i+3] > y)) Or ((polypoints[i+1] > y) And (polypoints[i+3] <= y))) ThenLocal vt:Float = (y - polypoints[i+1]) / (polypoints[i+3] - polypoints[i+1])If (x < polypoints[i] + vt * (polypoints[i+2] - polypoints[i])) Thencn = 1 ~ cnEndElseif (polypoints[i+1]<y And polypoints[j+1]>=y) Or (polypoints[j+1]< y And polypoints[i+1]>=y)If (polypoints[i]<=x Or polypoints[j]<=x)If (polypoints[i]+(y-polypoints[i+1])/(polypoints[j+1]-polypoints[i+1])*(polypoints[j]-polypoints[i])<x)cn2 = 1 ~ cn2EndifEndifEnd' corners at the bottemIf polypoints[i]=x And polypoints[i+1]=y Thencn = 1 ~ cnEndj=iNextReturn (cn&1)+(cn2&1)End FunctionI use that one, I dunno which one is faster.
October 1, 2016 at 9:34 am #4196I’d be happy to test spiral again, if you would like to recomplie it with MX 1.07
Yeah, I will do some recompiles and let you know when I have done it. Thank you!
October 1, 2016 at 11:36 am #4197Ok, I recompiled Spiral: http://www.playniax.com/SAF/spiral/ with the latest Monkey2!
Maybe this solves previous problems… Please, let me know!
Use mouse to drag the ball!
October 1, 2016 at 8:31 pm #4211Now works in Safari, MacOS Sierra, Mac Mini lastest gen
Google Chrome says: “Exception thrown, see JavaScript console” that says:
Monkey1234567891011Uncaught abort() at Errorat jsStackTrace (http://www.playniax.com/SAF/spiral/spiral.js:1:28519)at stackTrace (http://www.playniax.com/SAF/spiral/spiral.js:1:28702)at Object.abort (http://www.playniax.com/SAF/spiral/spiral.js:36:30558)at _abort (http://www.playniax.com/SAF/spiral/spiral.js:1:221083)at Array.Ooa (http://www.playniax.com/SAF/spiral/spiral.js:17:76866)at allocate (http://www.playniax.com/SAF/spiral/spiral.js:1:19287)at Object.callMain (http://www.playniax.com/SAF/spiral/spiral.js:36:28561)at doRun (http://www.playniax.com/SAF/spiral/spiral.js:36:29524)at http://www.playniax.com/SAF/spiral/spiral.js:36:29674If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.[EDIT] : A reload of the page solved the error… ?
October 1, 2016 at 8:51 pm #4213Works just fine on Windows 10 using Chrome.
October 1, 2016 at 8:52 pm #4214That would look great with some bumpmapping I think, not that it doesn’t look great already!
You’d just have to bumpmap the backdrop and use AddLight instead of DrawImage for the slowly rotating light/shadow image. Maybe set LightDepth quite high too.
October 5, 2016 at 3:22 pm #4279That would look great with some bumpmapping I think, not that it doesn’t look great already!
It probably would. I will ask Peter if we can pimp it up a bit… I am porting the Duel demo from Pyro MX1 right now this will be using your new lightning system
Anyway, I recompiled the other demos with the latest version of Monkey2 as well so please be my guest and test them. Hopefully like the Spiral demo they run without problems now…
http://www.playniax.com/SAF/jungle/
http://www.playniax.com/SAF/gz/
http://www.playniax.com/SAF/isotest/ -
AuthorPosts
You must be logged in to reply to this topic.