예제 #1
0
        public static Option <IRestInfo> GetRestInfo(
            this World world,
            PhysicsShapeQueryParameters shape)
        {
            Ensure.That(world, nameof(world)).IsNotNull();

            var state  = world.DirectSpaceState;
            var result = state.GetRestInfo(shape);

            return(result.Contains("collider") ? Some((IRestInfo) new RestInfo(result)) : None);
        }
예제 #2
0
        public static RestInfo GetRestInfo(
            [NotNull] this World world,
            [NotNull] PhysicsShapeQueryParameters shape)
        {
            Ensure.Any.IsNotNull(world, nameof(world));
            Ensure.Any.IsNotNull(shape, nameof(shape));

            var state  = world.DirectSpaceState;
            var result = state.GetRestInfo(shape);

            return(result.ContainsKey("collider") ? new RestInfo(result) : null);
        }
예제 #3
0
        public static IEnumerable <ICollision> CollideShape(
            this World world,
            PhysicsShapeQueryParameters shape,
            int maxResults = 32)
        {
            Ensure.That(world, nameof(world)).IsNotNull();

            return(world.DirectSpaceState
                   .CollideShape(shape, maxResults)
                   .Cast <IDictionary>()
                   .Filter(d => d.Contains("collider"))
                   .Map(d => (ICollision) new Collision(d)));
        }
예제 #4
0
        public static IEnumerable <Collision> CollideShape(
            [NotNull] this World world,
            [NotNull] PhysicsShapeQueryParameters shape,
            int maxResults = 32)
        {
            Ensure.Any.IsNotNull(world, nameof(world));
            Ensure.Any.IsNotNull(shape, nameof(shape));

            return(world.DirectSpaceState
                   .CollideShape(shape, maxResults)
                   .Cast <Dictionary <object, object> >()
                   .Where(d => d.ContainsKey("collider"))
                   .Select(d => new Collision(d)));
        }
예제 #5
0
    protected void FindRadius(Player ignore, float damage)
    {
        // test for radius damage
        SphereShape s = new SphereShape();

        s.SetRadius(_areaOfEffectRadius);

        // Get space and state of the subject body
        RID space = PhysicsServer.BodyGetSpace(this.GetRid());
        PhysicsDirectSpaceState state = PhysicsServer.SpaceGetDirectState(space);

        // Setup shape query parameters
        PhysicsShapeQueryParameters par = new PhysicsShapeQueryParameters();

        par.SetShapeRid(s.GetRid());
        par.Transform = this.Transform;

        Godot.Collections.Array result = state.IntersectShape(par);
        foreach (Dictionary <object, object> r in result)
        {
            if (r["collider"] is Player pl)
            {
                GD.Print("found player");
                if (pl != ignore || ignore == null)
                {
                    // find how far from explosion as a percentage, apply to damage
                    float dist = this.Transform.origin.DistanceTo(pl.Transform.origin);
                    dist = dist > this._areaOfEffectRadius ? (this._areaOfEffectRadius * .99f) : dist;
                    float pc = ((this._areaOfEffectRadius - dist) / this._areaOfEffectRadius);
                    float d  = damage * pc;
                    GD.Print("dam: " + damage);
                    GD.Print("pc: " + pc);
                    GD.Print("dist: " + dist);
                    GD.Print("aoe: " + this._areaOfEffectRadius);
                    GD.Print(this._playerOwner.GetName());
                    GD.Print(pl.GetName());
                    if (pl == this._playerOwner)
                    {
                        d = d * 0.5f;
                    }
                    GD.Print("inflicted dam: " + d);
                    // inflict damage
                    pl.TakeDamage(this.Transform, _weaponOwnerString, _weaponOwnerInflictLength, this._playerOwner, d);
                }
            }
        }
    }
예제 #6
0
파일: World.cs 프로젝트: lordee/godotfps
    // FIXME - increase max number of results
    // this has a max number of results that get filled with static bodies...
    public Godot.Collections.Array FindRadius(KinematicBody body, float radius)
    {
        // test for radius
        SphereShape s = new SphereShape();

        s.Radius = radius;

        // Get space and state of the subject body
        RID space = PhysicsServer.BodyGetSpace(body.GetRid());
        PhysicsDirectSpaceState state = PhysicsServer.SpaceGetDirectState(space);

        // Setup shape query parameters
        PhysicsShapeQueryParameters par = new PhysicsShapeQueryParameters();

        par.ShapeRid  = s.GetRid();
        par.Transform = body.Transform;

        return(state.IntersectShape(par, 100));
    }
예제 #7
0
    private void GetNeighboursAndObstaclesViaMesh()
    {
        neighbours = new List <Neighbour>();
        obstacles  = new List <Vector3>();

        var spaceState = GetWorld().DirectSpaceState;

        var perceptionShape = GetNode("perceptionMesh") as MeshInstance;

        if (perceptionShape == null)
        {
            return;
        }

        PhysicsShapeQueryParameters collisionShape = new PhysicsShapeQueryParameters();

        collisionShape.SetTransform(perceptionShape.GetGlobalTransform());;
        collisionShape.SetShape(perceptionShape.Mesh.CreateConvexShape());

        var result = spaceState.IntersectShape(collisionShape);

        foreach (Godot.Collections.Dictionary collision in result)
        {
            if (collision.ContainsKey("collider"))
            {
                if (collision["collider"] is Critter critter && critter != this)
                {
                    neighbours.Add(new Neighbour(critter));
                    continue;
                }
                else if (collision["collider"] is StaticBody staticBody && staticBody.IsInGroup("obstacles"))
                {
                    // Cast ray to object's origin to find where it hits
                    var rayResult = spaceState.IntersectRay(this.Transform.origin, staticBody.Transform.origin);

                    if (rayResult.Count > 0)
                    {
                        obstacles.Add((Vector3)rayResult["position"]);
                    }
                }
            }
        }
예제 #8
0
    protected Godot.Collections.Array FindPlayersInRadius()
    {
        SphereShape s = new SphereShape();

        s.SetRadius(_areaOfEffectRadius);

        // Get space and state of the subject body
        RID space = PhysicsServer.BodyGetSpace(this.GetRid());
        PhysicsDirectSpaceState state = PhysicsServer.SpaceGetDirectState(space);

        // Setup shape query parameters
        PhysicsShapeQueryParameters par = new PhysicsShapeQueryParameters();

        par.SetShapeRid(s.GetRid());
        par.Transform = this.Transform;

        Godot.Collections.Array result = state.IntersectShape(par);

        return(result);
    }
예제 #9
0
    public bool PlaceBlock(byte blockId)
    {
        var hitInfo = GetHitInfo();

        if (hitInfo != null)
        {
            Vector3    pos      = (Vector3)hitInfo["position"] + (Vector3)hitInfo["normal"] * 0.5f * Block.SIZE;
            IntVector3 blockPos = new IntVector3((int)Mathf.Round(pos.x / Block.SIZE), (int)Mathf.Round(pos.y / Block.SIZE), (int)Mathf.Round(pos.z / Block.SIZE));

            Vector3 blockCollisionPos = new Vector3(blockPos.x, blockPos.y, blockPos.z) * Block.SIZE;

            BoxShape bs = new BoxShape();
            bs.SetExtents(new Vector3(Block.SIZE, Block.SIZE, Block.SIZE) / 2);

            PhysicsShapeQueryParameters psqp = new PhysicsShapeQueryParameters();
            psqp.SetShape(bs);
            Transform t = new Transform(new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, 0.0f, 0.0f)).Translated(blockCollisionPos);
            psqp.SetTransform(t);

            object[] res = spaceState.IntersectShape(psqp);

            if (res.Length > 0)
            {
                for (int i = 0; i < res.Length; i++)
                {
                    Dictionary <object, object> info = (Dictionary <object, object>)spaceState.IntersectShape(psqp)[i];

                    if (info["collider"] is KinematicBody)
                    {
                        // A moving body (player, animal etc.) is in the way
                        return(false);
                    }
                }
            }

            terrain.SetBlock(blockPos, blockId);
            return(true);
        }
        return(false);
    }
예제 #10
0
        public override void _PhysicsProcess(float delta)
        {
            if (State == DoorState.Opened)
            {
                var space = _cellBody.GetWorld().DirectSpaceState;

                using (var query = new PhysicsShapeQueryParameters())
                {
                    query.SetShape(_cellShape.Shape);
                    query.CollisionMask     = (int)Level.CollisionLayers.Characters;
                    query.CollideWithBodies = true;
                    query.CollideWithAreas  = false;
                    query.Transform         = _cellShape.GlobalTransform;

                    using (var results = space.IntersectShape(query))
                    {
                        _canClose = (results == null || results.Count < 1);
                    }
                }
            }

            base._PhysicsProcess(delta);
        }