public override void _PhysicsProcess(float delta) { if (play) { float leftMove = (leftPaddleUp) ? -paddleSpeed : (leftPaddleDown) ? paddleSpeed : 0; float rightPaddleOffset = MapValue(18, 342, -20, 20, leftPaddle.Position.y); Vector2 rightMove = new Vector2(0, ball.Position.y - rightPaddle.Position.y + rightPaddleOffset); rightMove *= ballSpeed / 10; if (Math.Abs(rightMove.y) > paddleSpeed) { int sign = Math.Sign(rightMove.y); if (sign == -1) { rightMove.y = -paddleSpeed; } else { rightMove.y = paddleSpeed; } } leftPaddle.MoveAndCollide(new Vector2(0, leftMove) * delta); rightPaddle.MoveAndCollide(rightMove * delta); KinematicCollision2D col = ball.MoveAndCollide(ballVel * delta); if (col != null) { int beepIndex = prng.Next(0, 4); soundPlayer.SetStream(soundEffects[beepIndex]); soundPlayer.Play(); if (col.Collider == leftPaddle) { ballVel = ball.Position - leftPaddle.Position; ballSpeed += 10f; ballVel = ballVel.Normalized() * ballSpeed; shooter = (Math.Sign(ballVel.x) == -1) ? true : false; } else if (col.Collider == rightPaddle) { ballVel = ball.Position - rightPaddle.Position; ballSpeed += 25f; ballVel = ballVel.Normalized() * ballSpeed; shooter = (Math.Sign(ballVel.x) == -1) ? true : false; } else { ballVel = ballVel.Bounce(col.Normal); ballVel = ballVel.Normalized() * ballSpeed; } } } }
public override void _PhysicsProcess(float delta) { GD.Print($"has target {_hasTarget} – _velocity {_velocity} - target {_target}"); if (_hasTarget) { _velocity = GetVelocityToTarget(); GD.Print($"veloToTarget {_velocity}"); } var collision = _kinematicBody.MoveAndCollide(_velocity * delta); if (collision != null) { _velocity = NewVelocity(); GD.Print($"veloAfterCollison {_velocity}"); } }
private void _DoPhysics(KinematicBody2D parent, float delta) { _UpdateRotation(parent, delta); _UpdateVelocity(delta); var collision = parent.MoveAndCollide(Velocity.Rotated(parent.Rotation) * delta); if (collision != null) { EmitSignal(nameof(OnCollision), collision); Velocity = Vector2.Zero; } if (Break && Velocity.LengthSquared() <= 100000) { Velocity = Velocity.LinearInterpolate(Vector2.Zero, 0.3f); } }
public virtual void _ApplyCollideSteering(Vector3 accel, float delta) { KinematicBody2D _body = (KinematicBody2D)_body_ref.GetRef(); if (_body == null) { return; } var velocity = Utils.Clampedv3(linear_velocity + accel * delta, linear_speed_max); if (apply_linear_drag) { velocity = velocity.LinearInterpolate(Vector3.Zero, linear_drag_percentage); } _body.MoveAndCollide(Utils.ToVector2(velocity) * delta); if (calculate_velocities) { linear_velocity = velocity; } }
public override void _Process(float delta) { _timeSincePreviousStep += delta; if (_timeSincePreviousStep < _timeUntilNextStep) { return; } _timeSincePreviousStep = 0; if (Input.IsActionPressed("Left") && _direction != Direction.Right) { _direction = Direction.Left; } else if (Input.IsActionPressed("Right") && _direction != Direction.Left) { _direction = Direction.Right; } else if (Input.IsActionPressed("Up") && _direction != Direction.Down) { _direction = Direction.Up; } else if (Input.IsActionPressed("Down") && _direction != Direction.Up) { _direction = Direction.Down; } Vector2 previousPosition = _head.Position; KinematicCollision2D collision = null; switch (_direction) { case Direction.Left: collision = _head.MoveAndCollide(new Vector2(-_sprite.Texture.GetWidth(), 0)); break; case Direction.Right: collision = _head.MoveAndCollide(new Vector2(_sprite.Texture.GetWidth(), 0)); break; case Direction.Up: collision = _head.MoveAndCollide(new Vector2(0, -_sprite.Texture.GetHeight())); break; case Direction.Down: collision = _head.MoveAndCollide(new Vector2(0, _sprite.Texture.GetHeight())); break; } UpdateBodyPosition(0, previousPosition); if (collision != null) { EmitSignal(nameof(OnHitWall)); } if (IsHeadInBody()) { EmitSignal(nameof(OnBitBody)); } }