Example #1
0
        public override byte[] Render(bool toRgbArray = false)
        {
            if (Viewer == null)
            {
                Viewer = new Rendering.Viewer(VIEWPORT_W, VIEWPORT_H);
                Viewer.SetBounds(0, VIEWPORT_W / SCALE, 0, VIEWPORT_H / SCALE);
            }

            foreach (var obj in particles)
            {
                var data = obj.GetUserData() as CustomBodyData;
                data.TimeToLive -= 0.15f;
                data.Color1      = new b2Vec3((float)Math.Max(0.2, 0.2 + data.TimeToLive), (float)Math.Max(0.2, 0.5 * data.TimeToLive), (float)Math.Max(0.2, 0.5 * data.TimeToLive));
                data.Color2      = new b2Vec3((float)Math.Max(0.2, 0.2 + data.TimeToLive), (float)Math.Max(0.2, 0.5 * data.TimeToLive), (float)Math.Max(0.2, 0.5 * data.TimeToLive));
            }

            CleanParticles(false);

            foreach (var p in sky_polys)
            {
                Viewer.DrawPolygon(p).SetColor(0, 0, 0);
            }

            foreach (var obj in particles.Concat(drawlist))
            {
                for (b2Fixture f = obj.GetFixtureList(); f != null; f = f.GetNext())
                {
                    var xform = f.GetBody().GetTransform();
                    if (f.GetShape() is b2CircleShape)
                    {
                        var shape      = (f.GetShape() as b2CircleShape);
                        var customData = (obj.GetUserData() as CustomBodyData);
                        var trans      = Utils.b2Mul(xform, shape.m_p);

                        var t = new Rendering.Transform(new float[] { trans[0], trans[1] });
                        Viewer.DrawCircle(shape.m_radius, 20).SetColor(customData.Color1.x, customData.Color1.y, customData.Color1.z).AddAttr(t);
                        Viewer.DrawCircle(shape.m_radius, 20, false).SetColor(customData.Color2.x, customData.Color2.y, customData.Color2.z).SetLineWidth(2).AddAttr(t);
                    }
                    else
                    {
                        var shape      = f.GetShape();
                        var customData = (obj.GetUserData() as CustomBodyData);

                        var path = shape.GetVertices().Select(v => { var trans = Utils.b2Mul(xform, v); return(new float[] { trans[0], trans[1] }); }).ToList();
                        Viewer.DrawPolygon(path).SetColor(customData.Color1.x, customData.Color1.y, customData.Color1.z);
                        path.Add(path[0]);
                        Viewer.DrawPolyline(path).SetColor(customData.Color2.x, customData.Color2.y, customData.Color2.z).SetLineWidth(2);
                    }
                }
            }


            foreach (var x in new[] { helipad_x1, helipad_x2 })
            {
                var flagy1 = helipad_y;
                var flagy2 = flagy1 + 50 / SCALE;
                Viewer.DrawPolyline(new List <float[]> {
                    new float[] { x, flagy1 }, new float[] { x, flagy2 }
                }).SetColor(1, 1, 1);
                Viewer.DrawPolygon(new List <float[]> {
                    new float[] { x, flagy2 }, new float[] { x, flagy2 - 10 / SCALE }, new float[] { x + 25 / SCALE, flagy2 - 5 / SCALE }
                }).SetColor(0.8f, 0.8f, 0);
            }

            Viewer.Render();
            return(null);
        }
Example #2
0
        public override byte[] Render(bool toRgbArray = false)
        {
            if (Viewer == null)
            {
                Viewer = new Rendering.Viewer(VIEWPORT_W, VIEWPORT_H);
            }
            Viewer.SetBounds(scroll, VIEWPORT_W / SCALE + scroll, 0, VIEWPORT_H / SCALE);

            Viewer.DrawPolygon(new List <float[]> {
                new float[] { scroll, 0 },
                new float[] { scroll + VIEWPORT_W / SCALE, 0 },
                new float[] { scroll + VIEWPORT_W / SCALE, VIEWPORT_H / SCALE },
                new float[] { scroll, VIEWPORT_H / SCALE }
            }).SetColor(0.9f, 0.9f, 1.0f);

            foreach (var cloud_data in cloud_poly)
            {
                var poly     = cloud_data.Item1;
                var pos_data = cloud_data.Item2;

                if (pos_data.y < scroll / 2)
                {
                    continue;
                }
                if (pos_data.x > scroll / 2 + VIEWPORT_W / SCALE)
                {
                    continue;
                }
                Viewer.DrawPolygon(poly.Select(p => new[] { p[0] + scroll / 2, p[1] }).ToList()).SetColor(1, 1, 1);
            }
            foreach (var terrain_data in terrain_poly)
            {
                var poly  = terrain_data.Item1;
                var color = terrain_data.Item2;

                if (poly[1][0] < scroll)
                {
                    continue;
                }
                if (poly[0][0] > scroll + VIEWPORT_W / SCALE)
                {
                    continue;
                }
                Viewer.DrawPolygon(poly).SetColor(color.x, color.y, color.z);
            }

            lidar_render = (lidar_render + 1) % 100;
            int i = lidar_render;

            if (i < 2 * lidar.Length)
            {
                var l = i < lidar.Length ? lidar[i] : lidar[2 * lidar.Length - i - 1];
                Viewer.DrawPolyline(new List <float[]> {
                    new float[] { l.P1.x, l.P1.y }, new float[] { l.P2.x, l.P2.y }
                }).SetColor(1, 0, 0).SetLineWidth(1);
            }

            foreach (var obj in drawlist)
            {
                for (b2Fixture f = obj.GetFixtureList(); f != null; f = f.GetNext())
                {
                    var xform = f.GetBody().GetTransform();

                    if (f.GetShape() is b2CircleShape)
                    {
                        var shape      = (f.GetShape() as b2CircleShape);
                        var customData = (obj.GetUserData() as CustomBodyData);
                        var trans      = Utils.b2Mul(xform, shape.m_p);

                        var t = new Rendering.Transform(new float[] { trans[0], trans[1] });
                        Viewer.DrawCircle(shape.m_radius, 30).SetColor(customData.Color1.x, customData.Color1.y, customData.Color1.z).AddAttr(t);
                        Viewer.DrawCircle(shape.m_radius, 30, false).SetColor(customData.Color2.x, customData.Color2.y, customData.Color2.z).SetLineWidth(2).AddAttr(t);
                    }
                    else
                    {
                        var shape      = f.GetShape();
                        var customData = (obj.GetUserData() as CustomBodyData);

                        var path = shape.GetVertices().Select(v => { var trans = Utils.b2Mul(xform, v); return(new float[] { trans[0], trans[1] }); }).ToList();
                        Viewer.DrawPolygon(path).SetColor(customData.Color1.x, customData.Color1.y, customData.Color1.z);
                        path.Add(path[0]);
                        Viewer.DrawPolyline(path).SetColor(customData.Color2.x, customData.Color2.y, customData.Color2.z).SetLineWidth(2);
                    }
                }
            }

            var flagy1 = TERRAIN_HEIGHT;
            var flagy2 = flagy1 + 50 / SCALE;
            var x      = TERRAIN_STEP * 3;

            Viewer.DrawPolyline(new List <float[]> {
                new float[] { x, flagy1 }, new float[] { x, flagy2 }
            }).SetColor(0, 0, 0).SetLineWidth(2);
            var flagVert = new List <float[]> {
                new float[] { x, flagy2 }, new float[] { x, flagy2 - 10 / SCALE }, new float[] { x + 25 / SCALE, flagy2 - 5 / SCALE }
            };

            Viewer.DrawPolygon(flagVert).SetColor(0.9f, 0.2f, 0);
            flagVert.Add(flagVert[0]);
            Viewer.DrawPolyline(flagVert).SetColor(0, 0, 0).SetLineWidth(2);

            Viewer.Render();
            return(null);
        }
 public override void Step(b2TimeStep step)
 {
     if (m_bodyList == null)
     {
         return;
     }
     if (useWorldGravity)
     {
         gravity = GetWorld().GetGravity().Copy();
     }
     for (b2ControllerEdge i = m_bodyList; i != null; i = i.nextBody)
     {
         b2Body body = i.body;
         if (body.IsAwake() == false)
         {
             //Buoyancy force is just a function of position,
             //so unlike most forces, it is safe to ignore sleeping bodes
             continue;
         }
         b2Vec2 areac = new b2Vec2();
         b2Vec2 massc = new b2Vec2();
         float  area  = 0.0f;
         float  mass  = 0.0f;
         for (b2Fixture fixture = body.GetFixtureList(); fixture != null; fixture = fixture.GetNext())
         {
             b2Vec2 sc    = new b2Vec2();
             float  sarea = fixture.GetShape().ComputeSubmergedArea(normal, offset, body.GetTransform(), sc);
             area    += sarea;
             areac.x += sarea * sc.x;
             areac.y += sarea * sc.y;
             float shapeDensity;
             if (useDensity)
             {
                 //TODO: Figure out what to do now density is gone
                 shapeDensity = 1.0f;
             }
             else
             {
                 shapeDensity = 1.0f;
             }
             mass    += sarea * shapeDensity;
             massc.x += sarea * sc.x * shapeDensity;
             massc.y += sarea * sc.y * shapeDensity;
         }
         areac.x /= area;
         areac.y /= area;
         massc.x /= mass;
         massc.y /= mass;
         if (area < float.MinValue)
         {
             continue;
         }
         //Buoyancy
         b2Vec2 buoyancyForce = gravity.GetNegative();
         buoyancyForce.Multiply(density * area);
         body.ApplyForce(buoyancyForce, massc);
         //Linear drag
         b2Vec2 dragForce = body.GetLinearVelocityFromWorldPoint(areac);
         dragForce.Subtract(velocity);
         dragForce.Multiply(-linearDrag * area);
         body.ApplyForce(dragForce, areac);
         //Angular drag
         //TODO: Something that makes more physical sense?
         body.ApplyTorque(-body.GetInertia() / body.GetMass() * area * body.GetAngularVelocity() * angularDrag);
     }
 }