Пример #1
0
        public void AddObject(Instance inst)
        {
            if (ContainsInstance(inst))
            {
                throw new PhysicsException("Object already in world.", "PhysicsWorld.AddObjecct()");
            }
            PhysicsBodyType phyType      = inst.PhysicsAttributes.PhysicsBodyType;
            BodyType        bodyType     = inst.PhysicsAttributes.BodyType;
            Size            bodySize     = inst.PhysicsAttributes.PhysicsBodySize;
            float           bodyDensity  = inst.PhysicsAttributes.Density;
            float           bodyRotation = inst.PhysicsVariables.Direction;
            float           bodyRadius   = inst.PhysicsAttributes.Radius;
            Vector2         bodyPosition = ConvertUnits.ToSimUnits(new Vector2(inst.Position.X, inst.Position.Y));

            Body body = null;

            body = phyType switch
            {
                PhysicsBodyType.Box => BodyFactory.CreateRectangle(_velcroWorld, ConvertUnits.ToSimUnits(bodySize.W), ConvertUnits.ToSimUnits(bodySize.H), bodyDensity, bodyPosition, bodyRotation, bodyType, inst),
                PhysicsBodyType.Circle => BodyFactory.CreateCircle(_velcroWorld, ConvertUnits.ToSimUnits(bodyRadius), bodyDensity, bodyPosition, bodyType, inst),
                _ => throw new PhysicsException("Invalid PhysicsBodyType.", "PhysicsWorld.AddObject()"),
            };
            BodyDefPair pair = new BodyDefPair(inst, body);

            _bodyDefPairs.Add(pair);
            Debug.Log("PhysicsWorld.AddObject()", $"Added object of type '{inst.BaseReference.ObjectName}' @ ({body.Position.X}, {body.Position.Y})[GamePos: {ConvertUnits.ToDisplayUnits(body.Position.X)}, {ConvertUnits.ToDisplayUnits(body.Position.Y)}]");
        }
Пример #2
0
 public PhysicsAttributes(BodyType bodyType, int width, int height, float density)
 {
     PhysicsBodyType = PhysicsBodyType.Box;
     BodyType        = bodyType;
     PhysicsBodySize = new Size(width, height);
     Density         = density;
 }
Пример #3
0
 public PhysicsAttributes(BodyType bodyType, float radius, float density)
 {
     PhysicsBodyType = PhysicsBodyType.Circle;
     BodyType        = bodyType;
     PhysicsBodySize = new Size(0, 0);
     Radius          = radius;
     Density         = density;
 }
Пример #4
0
 public PhysicsAttributes(PhysicsAttributes copy)
 {
     BodyType        = copy.BodyType;
     PhysicsBodySize = copy.PhysicsBodySize;
     PhysicsBodyType = copy.PhysicsBodyType;
     Density         = copy.Density;
     Radius          = copy.Radius;
 }
Пример #5
0
 public PhysicsAttributes()
 {
     PhysicsBodyType = PhysicsBodyType.Box;
     BodyType        = BodyType.Static;
     PhysicsBodySize = new Size();
     Density         = 0f;
     Radius          = 0f;
 }
Пример #6
0
        public void DrawCollision(Instance inst, int sceneX, int sceneY)
        {
            PhysicsBodyType phyBody = inst.PhysicsAttributes.PhysicsBodyType;
            Coord           origin  = inst.Position;
            Coord           scene   = new Coord(sceneX, sceneY);

            Coord     position = new Coord(origin.X - scene.X, origin.Y - scene.Y);
            ColorRGBA def      = GetRendererDrawColor();

            SetRenderDrawColor(new ColorRGBA(255, 0, 0, 255));
            switch (phyBody)
            {
            case PhysicsBodyType.Box:
                int      w    = inst.PhysicsAttributes.PhysicsBodySize.W / 2;
                int      h    = inst.PhysicsAttributes.PhysicsBodySize.H / 2;
                SDL_Rect rect = new SDL_Rect();
                rect.x = position.X - w;
                rect.y = position.Y - h;
                rect.w = w * 2;
                rect.h = h * 2;
                if (SDL_RenderDrawRect(Renderer, ref rect) != 0)
                {
                    Debug.Log("GraphicsEngine.DrawCollision", $"Could not draw collision rect for instance: {inst.Hash}");
                }
                //Debug.Log($"Drawing coll-box on: ({rect.x}, {rect.y})[{rect.w}x{rect.h}]");
                break;

            case PhysicsBodyType.Circle:
                if (SDL_RenderDrawCircle(Renderer, position.X, position.Y, Convert.ToInt32(inst.PhysicsAttributes.Radius)) != 0)
                {
                    Debug.Log("GraphicsEngine.DrawCollision", $"Could not draw collision circle for instance: {inst.Hash}");
                }
                break;
            }
            SetRenderDrawColor(def);
        }