Thrown Item Physics (#120)

* Thrown object speed is now based on force.
Fixed BoundingBox namespace.

* 100% more throwing.
CollisionGroup enum.

* Update Engine.
This commit is contained in:
Acruid
2018-10-30 01:13:10 -07:00
committed by Pieter-Jan Briers
parent fc7493e149
commit d186f18c20
8 changed files with 119 additions and 33 deletions

View File

@@ -90,6 +90,7 @@
<Compile Include="GameObjects\Components\Power\PowerStorageNetComponent.cs" />
<Compile Include="GameObjects\Components\Power\PowerTransferComponent.cs" />
<Compile Include="GameObjects\Components\Projectiles\ProjectileComponent.cs" />
<Compile Include="GameObjects\Components\Projectiles\ThrownItemComponent.cs" />
<Compile Include="GameObjects\Components\Weapon\Melee\MeleeWeaponComponent.cs" />
<Compile Include="GameObjects\Components\Weapon\Ranged\Hitscan\HitscanWeaponComponent.cs" />
<Compile Include="GameObjects\Components\Weapon\Ranged\Projectile\ProjectileWeapon.cs" />

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects;
using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Power;
using Content.Server.GameObjects.Components.Interactable.Tools;
using Content.Server.Interfaces.GameObjects;
@@ -104,6 +105,7 @@ namespace Content.Server
factory.Register<HitscanWeaponComponent>();
factory.Register<ProjectileWeaponComponent>();
factory.Register<ProjectileComponent>();
factory.Register<ThrownItemComponent>();
factory.Register<MeleeWeaponComponent>();
factory.Register<HandheldLightComponent>();

View File

@@ -0,0 +1,36 @@
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Projectiles;
using Content.Shared.Physics;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.GameObjects.Components;
namespace Content.Server.GameObjects.Components
{
class ThrownItemComponent : ProjectileComponent, ICollideBehavior
{
public override string Name => "ThrownItem";
void ICollideBehavior.CollideWith(List<IEntity> collidedwith)
{
foreach (var entity in collidedwith)
{
if (entity.TryGetComponent(out DamageableComponent damage))
{
damage.TakeDamage(DamageType.Brute, 10);
}
}
// Stop colliding with mobs, this mimics not having enough velocity to do damage
// after impacting the first object.
// For realism this should actually be changed when the velocity of the object is less than a threshold.
// This would allow ricochets off walls, and weird gravity effects from slowing the object.
if (collidedwith.Count > 0 && Owner.TryGetComponent(out ICollidableComponent body))
{
body.CollisionMask &= (int) ~CollisionGroup.Mob;
// KYS, your job is finished.
Owner.RemoveComponent<ThrownItemComponent>();
}
}
}
}

View File

@@ -14,7 +14,6 @@ using SS14.Server.GameObjects;
using SS14.Server.GameObjects.EntitySystems;
using SS14.Server.Interfaces.Player;
using SS14.Shared.Interfaces.GameObjects.Components;
using SS14.Shared.GameObjects.Components.BoundingBox;
using SS14.Shared.Players;
namespace Content.Server.GameObjects.EntitySystems

View File

@@ -1,6 +1,9 @@
using System;
using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Projectiles;
using Content.Server.GameObjects.Components.Stack;
using Content.Shared.Input;
using Content.Shared.Physics;
using SS14.Server.GameObjects;
using SS14.Server.GameObjects.EntitySystems;
using SS14.Server.Interfaces.Player;
@@ -8,7 +11,10 @@ using SS14.Shared.GameObjects;
using SS14.Shared.GameObjects.EntitySystemMessages;
using SS14.Shared.GameObjects.Systems;
using SS14.Shared.Input;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.GameObjects.Components;
using SS14.Shared.Interfaces.Timing;
using SS14.Shared.IoC;
using SS14.Shared.Map;
using SS14.Shared.Maths;
using SS14.Shared.Players;
@@ -17,7 +23,7 @@ namespace Content.Server.GameObjects.EntitySystems
{
internal class HandsSystem : EntitySystem
{
private const float ThrowSpeed = 1.0f;
private const float ThrowForce = 1.5f; // Throwing force of mobs in Newtons
/// <inheritdoc />
public override void Initialize()
@@ -133,14 +139,40 @@ namespace Content.Server.GameObjects.EntitySystems
if (!plyEnt.TryGetComponent(out HandsComponent handsComp))
return;
if (handsComp.CanDrop(handsComp.ActiveIndex))
{
var throwEnt = handsComp.GetHand(handsComp.ActiveIndex).Owner;
handsComp.Drop(handsComp.ActiveIndex, null);
if (!handsComp.CanDrop(handsComp.ActiveIndex))
return;
if (!throwEnt.TryGetComponent(out ProjectileComponent projComp))
var throwEnt = handsComp.GetHand(handsComp.ActiveIndex).Owner;
// pop off an item, or throw the single item in hand.
if (!throwEnt.TryGetComponent(out StackComponent stackComp) || stackComp.Count < 2)
{
projComp = throwEnt.AddComponent<ProjectileComponent>();
handsComp.Drop(handsComp.ActiveIndex, null);
}
else
{
stackComp.Use(1);
throwEnt = throwEnt.EntityManager.ForceSpawnEntityAt(throwEnt.Prototype.ID, plyEnt.Transform.LocalPosition);
}
if (!throwEnt.TryGetComponent(out CollidableComponent colComp))
{
colComp = throwEnt.AddComponent<CollidableComponent>();
if(!colComp.Running)
colComp.Startup();
}
colComp.CollisionEnabled = true;
colComp.CollisionLayer |= (int)CollisionGroup.Items;
colComp.CollisionMask |= (int)CollisionGroup.Grid;
// I can now collide with player, so that i can do damage.
colComp.CollisionMask |= (int) CollisionGroup.Mob;
if (!throwEnt.TryGetComponent(out ThrownItemComponent projComp))
{
projComp = throwEnt.AddComponent<ThrownItemComponent>();
}
projComp.IgnoreEntity(plyEnt);
@@ -153,22 +185,20 @@ namespace Content.Server.GameObjects.EntitySystems
physComp = throwEnt.AddComponent<PhysicsComponent>();
}
physComp.LinearVelocity = dirVec * ThrowSpeed;
// TODO: Move this into PhysicsSystem, we need an ApplyForce function.
var a = ThrowForce / (float) Math.Max(0.001, physComp.Mass); // a = f / m
var timing = IoCManager.Resolve<IGameTiming>();
var spd = a / (1f / timing.TickRate); // acceleration is applied in 1 tick instead of 1 second, scale appropriately
physComp.LinearVelocity = dirVec * spd;
var wHomoDir = Vector3.UnitX;
transform.InvWorldMatrix.Transform(ref wHomoDir, out var lHomoDir);
lHomoDir.Normalize();
var angle = new Angle(lHomoDir.Xy);
transform.LocalRotation = angle;
}
else
{
return;
}
transform.LocalRotation = new Angle(lHomoDir.Xy);
}
}
}

View File

@@ -71,6 +71,7 @@
<Compile Include="GameObjects\Components\Storage\SharedStorageComponent.cs" />
<Compile Include="GameObjects\ContentNetIDs.cs" />
<Compile Include="GameObjects\PhysicalConstants.cs" />
<Compile Include="Physics\CollisionGroup.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GameObjects\Components\Items\SharedHandsComponent.cs" />
<Compile Include="GameObjects\Components\Power\SharedPowerDebugTool.cs" />

View File

@@ -0,0 +1,17 @@
using System;
namespace Content.Shared.Physics
{
/// <summary>
/// Defined collision groups for the physics system.
/// </summary>
[Flags]
public enum CollisionGroup
{
None = 0x0000,
Grid = 0x0001, // Walls
Mob = 0x0002, // Mobs, like the player or NPCs
Fixture = 0x0004, // wall fixtures, like APC or posters
Items = 0x008, // Items on the ground
}
}

2
engine

Submodule engine updated: 307030ec8f...5ae665c3d2