private int CheckCollisions(Vector2 StartPosition, Vector2 EndPosition) { int PossibleLength = 0; Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState; Vector2 Direction = (EndPosition - StartPosition).Normalized(); Vector2 rayEnd = (StartPosition + (Direction * (25 + (ChainLength * LengthOfChainLink)))); Godot.Collections.Dictionary result = spaceState.IntersectRay(StartPosition, rayEnd, PlayerArr); Vector2 positionOfChainEnd = StartPosition; if (result.Count != 0) { Vector2 collisionPosition = (Vector2)result["position"]; double distanceBetween = Math.Sqrt(Math.Pow((collisionPosition.x - positionOfChainEnd.x), 2) + Math.Pow((collisionPosition.y - positionOfChainEnd.y), 2)); double unitsBetween = (distanceBetween / LengthOfChainLink); PossibleLength = Convert.ToInt32(Math.Floor(unitsBetween)); } else { PossibleLength = ChainLength; } return(PossibleLength); }
/// <summary> /// Uses raycasting to find if neighbours of provided points are valid /// </summary> /// <param name="point"> /// The Vector2 from which to find neighbours from<</param> /// <returns></returns> public List <Vector2> GetNeighbours(Vector2 point) { var targets = new[] { point + Vector2.Up, point + Vector2.Right, point + Vector2.Down, point + Vector2.Left, point + new Vector2(1, 1), //down right point + new Vector2(1, -1), // up right point + new Vector2(-1, 1), // down left point + new Vector2(-1, -1) // up left }; var validTargets = new List <Vector2>(); foreach (var target in targets) { var result = _space.IntersectRay(MapToWorld(point), MapToWorld(target), _excludes); // see if there's anything there then add it to the list of valid points if (result.Count == 0) { validTargets.Add(target); } } return(validTargets); }
public static Physics2DSpaceQueryRayResult IntersectPointStructured( this Physics2DDirectSpaceState state, Vector2 from, Vector2 to, Godot.Collections.Array exclude = null, uint collision_layer = 2147483647, bool collide_with_bodies = true, bool collide_with_areas = false ) => new Physics2DSpaceQueryRayResult(state.IntersectRay(from, to, exclude, collision_layer, collide_with_bodies, collide_with_areas));
private void Attack() { //Check if the target is still in the desired distane if (Position.DistanceTo(target.Position) > 300) { //Change the state to the attack state myState = AIStates.MoveTo; } else { //Get the direction to move in Vector2 dir = target.Position - Position; //Normalizing direction for movement dir = dir.Normalized(); //gunSprite.Rotation = Mathf.LerpAngle(gunSprite.Rotation, dir.Angle(), 0.2f); gunSprite.LookAt(target.Position); } //If the attack timer has not run out yet we just return out of the method if (!canAttack) { return; } //Reset the can attack bool canAttack = false; //Start the attack timer attackTimer.Start(); //so I am brute forcing the heck out of this mthod sorry ShowFlash(); Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary Godot.Collections.Dictionary hits = worldState.IntersectRay(GlobalPosition, target.GlobalPosition, new Godot.Collections.Array { this }, this.CollisionMask); //Check if there was a hit if (hits.Count > 0) { if (hits.Contains("collider")) { UnitHitEvent uhei = new UnitHitEvent(); uhei.attacker = (Node2D)GetParent(); uhei.target = (Node2D)hits["collider"]; uhei.damage = 5; uhei.Description = uhei.attacker.Name + " attacked " + uhei.target.Name; uhei.FireEvent(); } } }
private void Fire() { /* * if (missileUpgrade) * { * missile = missileScene.Instance(); * * if (((Missile)missile).HasMethod("Start")) * { * //((Missile)missile).Start(this.Transform, null); * //((Missile)missile).Start(GetNode<Node2D>("../../../../../Main").Transform, null); * } * Node2D tempNode = GetNode<Node2D>("../../../../../Main/MissileContainer"); * tempNode.AddChild(missile); * //AddChild(missile); * * } * else * {*/ //Get a snapshot of the physics state of he world at this moment Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary Godot.Collections.Dictionary hits = worldState.IntersectRay(GlobalPosition, GetGlobalMousePosition(), new Godot.Collections.Array { tankBody }, tankBody.CollisionMask); //Check if there was a hit if (hits.Count > 0) { //Change the line2d end position if there was a hit //hitPos = (Vector2)hits["position"]; if (hits.Contains("collider")) { if (((Node)hits["collider"]).IsInGroup("Enemies")) { UnitHitEvent uhei = new UnitHitEvent(); uhei.attacker = (Node2D)Owner; uhei.target = (Node2D)hits["collider"]; uhei.damage = 50; uhei.Description = uhei.attacker.Name + " attacked " + uhei.target.Name; uhei.FireEvent(); } } } //Set the start and end of the line2D and then make it visible //traceLine.Points = new Vector2[] {Vector2.Zero, hitPos }; //traceLine.Visible = true; //traceTimer.Start(); //} }
private Node2D GetCollidingNode(Vector2 StartPosition, Vector2 EndPosition, Vector2 NodePosition) { Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState; Vector2 Direction = (EndPosition - StartPosition).Normalized(); Vector2 rayEnd = (StartPosition + (Direction * (25 + (ChainLength * LengthOfChainLink)))); Godot.Collections.Dictionary result = spaceState.IntersectRay(NodePosition, rayEnd, PlayerArr); if (result.Count != 0) { return((Node2D)result["collider"]); } return(null); }
public static Vector2 SimpleRayCast(CanvasItem spaceNode, Vector2 from, Vector2 to, out bool success) { Physics2DDirectSpaceState physicsState = Physics2DServer.SpaceGetDirectState(spaceNode.GetWorld2d().Space); Dictionary result = physicsState.IntersectRay(from, to, GodotUtility.MakeGDArray(spaceNode)); if (result.ContainsKey("position")) { success = true; return((Vector2)result["position"]); } else { success = false; return(new Vector2()); } }
private bool PlayerIsInLOS() { Physics2DDirectSpaceState space = GetWorld2d().DirectSpaceState; Godot.Collections.Array exclude = new Godot.Collections.Array { this }; Dictionary LOS_Obstacle = space.IntersectRay(GlobalPosition, _player.GlobalPosition, exclude, CollisionMask); float DistToPlayer = _player.GlobalPosition.DistanceTo(GlobalPosition); bool PlayerInRange = DistToPlayer < MAX_Range; if (LOS_Obstacle["collider"] == _player && PlayerInRange) { return(true); } return(false); }
public override void _PhysicsProcess(float delta) { if (!canFire) { return; } Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary //This works, why?!?!???!?!?!?!! //Why does it not work on the child object, is it becuse the child object is not centered Godot.Collections.Dictionary hits = worldState.IntersectRay(parentNode.GlobalPosition, parentNode.GlobalPosition + parentNode.Transform.x * maxDistance, new Godot.Collections.Array { hitBox }); //If there are no hits then we return out of the function if (hits.Count > 0) { Vector2 hitPos = (Vector2)hits["position"]; //Offset the start position of hte laser so that it looks like it is originating at the nozzle of the barrel laserBeam.SetPointPosition(0, ((Node2D)GetParent().GetParent()).Position); laserBeam.SetPointPosition(1, hitPos); BeamHitParticles.Emitting = true; BeamHitParticles.Position = hitPos; //Fire of the hit event HitEvent hei = new HitEvent(); hei.target = (Node2D)hits["collider"]; hei.attacker = (Node2D)GetParent(); hei.damage = 100; hei.FireEvent(); } else { BeamHitParticles.Emitting = false; //Works do not delete!!!!!!!!!!!!!!! //======================================== laserBeam.SetPointPosition(0, new Vector2(16, 0)); laserBeam.SetPointPosition(1, Position + Transform.x * maxDistance); //======================================== } }
public static RayCastHitInfo RayCastObject(CanvasItem spaceNode, Vector2 from, Vector2 to, out bool success) { Physics2DDirectSpaceState physicsState = Physics2DServer.SpaceGetDirectState(spaceNode.GetWorld2d().Space); Dictionary result = physicsState.IntersectRay(from, to, GodotUtility.MakeGDArray(spaceNode)); if (result.ContainsKey("position")) { success = true; RayCastHitInfo info = new RayCastHitInfo(); info.position = (Vector2)result["position"]; if (result.ContainsKey("collider")) { info.collider = (Godot.Object)result["collider"]; } return(info); } else { success = false; return(null); } }
private void lineOfSightCheck() { if (target == null) { return; } Physics2DDirectSpaceState worldState = GetWorld2d().DirectSpaceState; //Get the raycast hits and store them in a dictionary Godot.Collections.Dictionary hits = worldState.IntersectRay(this.GlobalPosition, ((KinematicBody2D)target).GlobalPosition); if (hits.Count > 0) { if (hits.Contains("collider")) { Node2D col = (Node2D)hits["collider"]; if (col.IsInGroup("Player")) { targetInLineOfSight = true; } } } }