|
@@ -316,15 +316,23 @@ For example, here is the code for an "Asteroids" style spaceship:
|
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
|
{
|
|
{
|
|
if (Input.IsActionPressed("ui_up"))
|
|
if (Input.IsActionPressed("ui_up"))
|
|
|
|
+ {
|
|
state.ApplyForce(_thrust.Rotated(Rotation));
|
|
state.ApplyForce(_thrust.Rotated(Rotation));
|
|
|
|
+ }
|
|
else
|
|
else
|
|
|
|
+ {
|
|
state.ApplyForce(new Vector2());
|
|
state.ApplyForce(new Vector2());
|
|
|
|
+ }
|
|
|
|
|
|
var rotationDir = 0;
|
|
var rotationDir = 0;
|
|
if (Input.IsActionPressed("ui_right"))
|
|
if (Input.IsActionPressed("ui_right"))
|
|
|
|
+ {
|
|
rotationDir += 1;
|
|
rotationDir += 1;
|
|
|
|
+ }
|
|
if (Input.IsActionPressed("ui_left"))
|
|
if (Input.IsActionPressed("ui_left"))
|
|
|
|
+ {
|
|
rotationDir -= 1;
|
|
rotationDir -= 1;
|
|
|
|
+ }
|
|
state.ApplyTorque(rotationDir * _torque);
|
|
state.ApplyTorque(rotationDir * _torque);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -441,7 +449,9 @@ Or to bounce off of the colliding object:
|
|
{
|
|
{
|
|
var collisionInfo = MoveAndCollide(_velocity * (float)delta);
|
|
var collisionInfo = MoveAndCollide(_velocity * (float)delta);
|
|
if (collisionInfo != null)
|
|
if (collisionInfo != null)
|
|
|
|
+ {
|
|
_velocity = _velocity.Bounce(collisionInfo.GetNormal());
|
|
_velocity = _velocity.Bounce(collisionInfo.GetNormal());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -510,11 +520,17 @@ the ground (including slopes) and jump when standing on the ground:
|
|
var jump = Input.IsActionPressed("ui_select");
|
|
var jump = Input.IsActionPressed("ui_select");
|
|
|
|
|
|
if (IsOnFloor() && jump)
|
|
if (IsOnFloor() && jump)
|
|
|
|
+ {
|
|
velocity.Y = _jumpSpeed;
|
|
velocity.Y = _jumpSpeed;
|
|
|
|
+ }
|
|
if (right)
|
|
if (right)
|
|
|
|
+ {
|
|
velocity.X += _runSpeed;
|
|
velocity.X += _runSpeed;
|
|
|
|
+ }
|
|
if (left)
|
|
if (left)
|
|
|
|
+ {
|
|
velocity.X -= _runSpeed;
|
|
velocity.X -= _runSpeed;
|
|
|
|
+ }
|
|
|
|
|
|
Velocity = velocity;
|
|
Velocity = velocity;
|
|
}
|
|
}
|