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); }
private void InternalEmit(int idx, EmitParams emitParams) { Particle p = particles[idx]; World2D world = GetWorld2d(); Physics2DDirectSpaceState spaceState = world.DirectSpaceState; foreach (ParticleSystem2DModule module in modules) { if (!module.enabled) { continue; } IParticleInitializable IInit = module as IParticleInitializable; if (IInit == null) { continue; } module.world = world; module.spaceState = spaceState; IInit.InitParticle(ref p, emitParams); } p.alive = true; particles[idx] = p; }
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 UpdateSystem(float delta) { try { if (timeScale < 0f) { timeScale = 0f; } delta *= timeScale; Vector2 deltaPos = GlobalPosition - prevPos; currentVelocity = deltaPos / delta; World2D world = GetWorld2d(); Physics2DDirectSpaceState spaceState = world.DirectSpaceState; foreach (ParticleSystem2DModule module in modules) { if (!module.enabled) { continue; } module.particleSystem = this; module.world = world; module.spaceState = spaceState; module.UpdateModule(delta); for (int i = 0; i < maxParticles; i++) { IParticleUpdateable IUpdate = module as IParticleUpdateable; if (particles[i].alive) { if (IUpdate != null) { IUpdate.UpdateParticle(ref particles[i], delta); } if (particles[i].currentLife <= 0f) { particles[i].currentLife = 0f; DestroyParticle(i); particles[i].alive = false; } } } } prevPos = GlobalPosition; } catch (Exception e) { if (Engine.EditorHint) { emitting = false; } var st = new System.Diagnostics.StackTrace(e, true); GD.PrintErr(st); } }
public override void _Ready() { //Setup worldSpace = GetWorld2d().DirectSpaceState; query = new Physics2DShapeQueryParameters(); inputDelayTimer = (Timer)GetNode("InputDelayTimer"); mouseClickMoveTolerance = GM.MouseClickMoveTolerance; inputDelayTimer.Start(); }
public static (float?furthestMove, float?collisionPoint) CastMotionTuple( this Physics2DDirectSpaceState state, Physics2DShapeQueryParameters param) { var motion = state.CastMotion(param); if (motion.Count == 0) { return(null, null); } return((float)motion[0], (float)motion[1]); }
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 HandleDropAt(Vector2 mousePosition) { if (this.activeDragNode is Grapheme dragNode) { Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState; Godot.Collections.Array phonemeCheck = spaceState.IntersectPoint(mousePosition, maxResults: 1, collideWithAreas: true, collisionLayer: 1); if (phonemeCheck.Count > 0) { Area2D phonemeCollider = (Area2D)((Godot.Collections.Dictionary)phonemeCheck[0])["collider"]; Phoneme phoneme = phonemeCollider.GetParent <Phoneme>(); phoneme.AcceptDrop(dragNode); } } }
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 Array <Physics2DSpaceQueryResult> IntersectPointStructured( this Physics2DDirectSpaceState state, Vector2 point, int maxResults = 32, Godot.Collections.Array exclude = null, uint collisionLayer = 2147483647, bool bodyCollide = true, bool areaCollide = false ) { var intersect = state.IntersectPoint(point, maxResults, exclude, collisionLayer, bodyCollide, areaCollide); var result = new Array <Physics2DSpaceQueryResult>(); result.Resize(intersect.Count); for (int i = 0; i < intersect.Count; i++) { result[i] = new Physics2DSpaceQueryResult((Dictionary)intersect[i]); } return(result); }
public override void _EnterTree() { World2D world = GetWorld2d(); Physics2DDirectSpaceState spaceState = world.DirectSpaceState; if (modulesData.Count == 0) { modules.Clear(); maxParticles = 256; seed = new Random().Next(); emitting = true; emitOnStart = true; AddModule <ParticleSystem2DEmitOptionsModule>(false); AddModule <ParticleSystem2DEmitRateModule>(false); AddModule <ParticleSystem2DUpdateModule>(false); AddModule <ParticleSystem2DDrawBatchModule>(false); WriteModulesData(); } else { if (modules.Count != modulesData.Count) { ReadModulesData(); } } if (!Engine.EditorHint) { emitting |= emitOnStart; } foreach (ParticleSystem2DModule module in modules) { module.particleSystem = this; module.InitModule(); } prevPos = GlobalPosition; }
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); } }
public override void _Input(InputEvent @event) { if (@event is InputEventMouseMotion eventMouseMotion && this.activeDragNode != this && this.activeDragNode is Grapheme dragNode) { dragNode.Translate(eventMouseMotion.Relative); } if (@event is InputEventMouseButton eventMouseButton && this.activeDragNode == this && eventMouseButton.Pressed == true && eventMouseButton.ButtonIndex == 1) { Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState; Godot.Collections.Array graphemeCheck = spaceState.IntersectPoint(eventMouseButton.Position, maxResults: 1, collideWithAreas: true, collisionLayer: 2); if (graphemeCheck.Count > 0) { Area2D graphemeCollider = (Area2D)((Godot.Collections.Dictionary)graphemeCheck[0])["collider"]; Grapheme grapheme = graphemeCollider.GetParent <Grapheme>(); this.Drag(grapheme); } } if (@event is InputEventMouseButton eventMouseButtonRelease && this.activeDragNode != this && eventMouseButtonRelease.Pressed == false && eventMouseButtonRelease.ButtonIndex == 1) { this.Drop(eventMouseButtonRelease.Position); } }
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; } } } }
public static Array <Vector2> CollideShapeGeneric( this Physics2DDirectSpaceState state, Physics2DShapeQueryParameters param, int maxResults = 32) => new Array <Vector2>(state.CollideShape(param, maxResults));
public RayCastAStar(TileMap tileMap, Physics2DDirectSpaceState space) { _tileMap = tileMap; _space = space; }
public override void _PhysicsProcess(float delta) { if ((Input.IsActionJustPressed("ui_mouse_click") || Input.IsActionJustPressed("ui_right_click")) && !Input.IsActionPressed("ui_shift")) { Physics2DDirectSpaceState spaceState = GetWorld2d().DirectSpaceState; Godot.Collections.Array result = spaceState.IntersectPoint(GetGlobalMousePosition(), 32, null, 1, false, true); if (result.Count == 0) { ClientVariables.SelectedTokens.Clear(); } else { Dictionary <int, Node> tokenDictionary = new Dictionary <int, Node>(); int topIndex = 0; foreach (Godot.Collections.Dictionary hit in result) { if (hit.Contains("collider")) { if (hit["collider"].GetType().Name == "Area2D") { Area2D collider = (Area2D)hit["collider"]; Node token = collider.GetParent().GetParent(); tokenDictionary.Add(token.GetIndex(), token); if (topIndex < token.GetIndex()) { topIndex = token.GetIndex(); } } } } if (tokenDictionary.Count > 0) { Node pressedToken = tokenDictionary[topIndex]; if (Input.IsActionPressed("ui_control")) { if (ClientVariables.SelectedTokens.Contains(pressedToken)) { ClientVariables.SelectedTokens.Remove(pressedToken); } else { ClientVariables.SelectedTokens.Add(pressedToken); } } else { bool found = false; foreach (Node selectedToken in ClientVariables.SelectedTokens) { if (tokenDictionary.ContainsValue(selectedToken)) { found = true; } } if (!found) { ClientVariables.SelectedTokens.Clear(); ClientVariables.SelectedTokens.Add(pressedToken); } } if (Input.IsActionJustPressed("ui_right_click")) { Token token = (Token)pressedToken; token.PopupMenu.Visible = true; } if (Input.IsActionJustPressed("ui_mouse_click")) { Token token = (Token)pressedToken; token.PopupMenu.Visible = false; } } } } }