From c743c7fd0cc12bf10d76ecb8cdcb8029ed8429a4 Mon Sep 17 00:00:00 2001 From: DevToaster Date: Mon, 30 Mar 2015 19:42:32 -0400 Subject: Modified physics for more vanilla-like behavior --- src/Entities/ArrowEntity.cpp | 4 ++++ src/Entities/Boat.cpp | 4 +++- src/Entities/Entity.cpp | 20 +++++++++++--------- src/Entities/Entity.h | 8 ++++++++ src/Entities/FallingBlock.cpp | 2 ++ src/Entities/FireChargeEntity.cpp | 1 + src/Entities/FireworkEntity.cpp | 2 ++ src/Entities/GhastFireballEntity.cpp | 1 + src/Entities/Minecart.cpp | 4 +++- src/Entities/Pawn.cpp | 2 ++ src/Entities/Pickup.cpp | 3 ++- src/Entities/ProjectileEntity.cpp | 4 ++++ src/Entities/TNTEntity.cpp | 4 ++++ src/Entities/WitherSkullEntity.cpp | 2 ++ src/Mobs/Blaze.cpp | 2 ++ 15 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index 3c1fabb1b..3516b7540 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -21,6 +21,8 @@ cArrowEntity::cArrowEntity(cEntity * a_Creator, double a_X, double a_Y, double a { SetSpeed(a_Speed); SetMass(0.1); + SetGravity(-20.0f); + SetAirDrag(0.2f); SetYawFromSpeed(); SetPitchFromSpeed(); LOGD("Created arrow %d with speed {%.02f, %.02f, %.02f} and rot {%.02f, %.02f}", @@ -48,6 +50,8 @@ cArrowEntity::cArrowEntity(cPlayer & a_Player, double a_Force) : { m_PickupState = psInCreative; } + SetGravity(-20.0f); + SetAirDrag(0.2f); } diff --git a/src/Entities/Boat.cpp b/src/Entities/Boat.cpp index 6177eb32f..8899921ab 100644 --- a/src/Entities/Boat.cpp +++ b/src/Entities/Boat.cpp @@ -16,7 +16,9 @@ cBoat::cBoat(double a_X, double a_Y, double a_Z) : super(etBoat, a_X, a_Y, a_Z, 0.98, 0.7) { - SetMass(20.f); + SetMass(20.0f); + SetGravity(-16.0f); + SetAirDrag(1.0f); SetMaxHealth(6); SetHealth(6); } diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index c8df6b4b1..67dae44b2 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -36,6 +36,7 @@ cEntity::cEntity(eEntityType a_EntityType, double a_X, double a_Y, double a_Z, d m_bHasSentNoSpeed(true), m_bOnGround(false), m_Gravity(-9.81f), + m_AirDrag(0.4f), m_LastPos(a_X, a_Y, a_Z), m_IsInitialized(false), m_WorldTravellingFrom(nullptr), @@ -943,6 +944,7 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { // Normal gravity fallspeed = m_Gravity * DtSec.count(); + NextSpeed -= NextSpeed * m_AirDrag * DtSec.count(); } NextSpeed.y += static_cast(fallspeed); } @@ -999,7 +1001,7 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) NextSpeed += m_WaterSpeed; - if (NextSpeed.SqrLength() > 0.f) + if (NextSpeed.SqrLength() > 0.0f) { cTracer Tracer(GetWorld()); // Distance traced is an integer, so we round up from the distance we should go (Speed * Delta), else we will encounter collision detection failurse @@ -1015,20 +1017,20 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) // Block hit was within our projected path // Begin by stopping movement in the direction that we hit something. The Normal is the line perpendicular to a 2D face and in this case, stores what block face was hit through either -1 or 1. // For example: HitNormal.y = -1 : BLOCK_FACE_YM; HitNormal.y = 1 : BLOCK_FACE_YP - if (Tracer.HitNormal.x != 0.f) + if (Tracer.HitNormal.x != 0.0f) { - NextSpeed.x = 0.f; + NextSpeed.x = 0.0f; } - if (Tracer.HitNormal.y != 0.f) + if (Tracer.HitNormal.y != 0.0f) { - NextSpeed.y = 0.f; + NextSpeed.y = 0.0f; } - if (Tracer.HitNormal.z != 0.f) + if (Tracer.HitNormal.z != 0.0f) { - NextSpeed.z = 0.f; + NextSpeed.z = 0.0f; } - if (Tracer.HitNormal.y == 1.f) // Hit BLOCK_FACE_YP, we are on the ground + if (Tracer.HitNormal.y == 1.0f) // Hit BLOCK_FACE_YP, we are on the ground { m_bOnGround = true; } @@ -1971,7 +1973,7 @@ void cEntity::SteerVehicle(float a_Forward, float a_Sideways) { return; } - if ((a_Forward != 0.f) || (a_Sideways != 0.f)) + if ((a_Forward != 0.0f) || (a_Sideways != 0.0f)) { m_AttachedTo->HandleSpeedFromAttachee(a_Forward, a_Sideways); } diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index 9bb1837f1..2c994c550 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -270,6 +270,10 @@ public: float GetGravity(void) const { return m_Gravity; } void SetGravity(float a_Gravity) { m_Gravity = a_Gravity; } + + float GetAirDrag(void) const { return m_AirDrag; } + + void SetAirDrag(float a_AirDrag) { m_AirDrag = a_AirDrag; } /// Sets the rotation to match the speed vector (entity goes "face-forward") void SetYawFromSpeed(void); @@ -504,6 +508,10 @@ protected: For realistic effects, this should be negative. For spaaaaaaace, this can be zero or even positive */ float m_Gravity; + /** Stores the air drag that is applied to the entity every tick, measured in speed ratio per second + Acts as air friction and slows down flight */ + float m_AirDrag; + /** Last position sent to client via the Relative Move or Teleport packets (not Velocity) Only updated if cEntity::BroadcastMovementUpdate() is called! */ Vector3d m_LastPos; diff --git a/src/Entities/FallingBlock.cpp b/src/Entities/FallingBlock.cpp index 7301a3c9d..82daeb4bf 100644 --- a/src/Entities/FallingBlock.cpp +++ b/src/Entities/FallingBlock.cpp @@ -16,6 +16,8 @@ cFallingBlock::cFallingBlock(const Vector3i & a_BlockPosition, BLOCKTYPE a_Block m_BlockMeta(a_BlockMeta), m_OriginalPosition(a_BlockPosition) { + SetGravity(-16.0f); + SetAirDrag(0.4f); } diff --git a/src/Entities/FireChargeEntity.cpp b/src/Entities/FireChargeEntity.cpp index aba32602f..f6c665156 100644 --- a/src/Entities/FireChargeEntity.cpp +++ b/src/Entities/FireChargeEntity.cpp @@ -12,6 +12,7 @@ cFireChargeEntity::cFireChargeEntity(cEntity * a_Creator, double a_X, double a_Y { SetSpeed(a_Speed); SetGravity(0); + SetAirDrag(0); } diff --git a/src/Entities/FireworkEntity.cpp b/src/Entities/FireworkEntity.cpp index 32eaf669a..89f69f113 100644 --- a/src/Entities/FireworkEntity.cpp +++ b/src/Entities/FireworkEntity.cpp @@ -13,6 +13,8 @@ cFireworkEntity::cFireworkEntity(cEntity * a_Creator, double a_X, double a_Y, do m_TicksToExplosion(a_Item.m_FireworkItem.m_FlightTimeInTicks), m_FireworkItem(a_Item) { + SetGravity(0); + SetAirDrag(0); } diff --git a/src/Entities/GhastFireballEntity.cpp b/src/Entities/GhastFireballEntity.cpp index 9e4cb387e..c64fb2a17 100644 --- a/src/Entities/GhastFireballEntity.cpp +++ b/src/Entities/GhastFireballEntity.cpp @@ -12,6 +12,7 @@ cGhastFireballEntity::cGhastFireballEntity(cEntity * a_Creator, double a_X, doub { SetSpeed(a_Speed); SetGravity(0); + SetAirDrag(0); } diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index ee10cf6b3..7fdb39ea7 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -92,7 +92,9 @@ cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) : m_DetectorRailPosition(0, 0, 0), m_bIsOnDetectorRail(false) { - SetMass(20.f); + SetMass(20.0f); + SetGravity(-16.0f); + SetAirDrag(1.0f); SetMaxHealth(6); SetHealth(6); SetWidth(1); diff --git a/src/Entities/Pawn.cpp b/src/Entities/Pawn.cpp index baf8a2f3b..a4a9c6f21 100644 --- a/src/Entities/Pawn.cpp +++ b/src/Entities/Pawn.cpp @@ -13,6 +13,8 @@ cPawn::cPawn(eEntityType a_EntityType, double a_Width, double a_Height) : super(a_EntityType, 0, 0, 0, a_Width, a_Height) , m_EntityEffects(tEffectMap()) { + SetGravity(-32.0f); + SetAirDrag(0.4f); } diff --git a/src/Entities/Pickup.cpp b/src/Entities/Pickup.cpp index 9f2609894..cec186b52 100644 --- a/src/Entities/Pickup.cpp +++ b/src/Entities/Pickup.cpp @@ -91,7 +91,8 @@ cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_It , m_bCollected(false) , m_bIsPlayerCreated(IsPlayerCreated) { - SetGravity(-10.5f); + SetGravity(-16.0f); + SetAirDrag(0.4f); SetMaxHealth(5); SetHealth(5); SetSpeed(a_SpeedX, a_SpeedY, a_SpeedZ); diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 4684e3e09..2b8573d4d 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -227,6 +227,8 @@ cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, double a ), m_IsInGround(false) { + SetGravity(-12.0f); + SetAirDrag(0.2f); } @@ -242,6 +244,8 @@ cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, const Ve SetSpeed(a_Speed); SetYawFromSpeed(); SetPitchFromSpeed(); + SetGravity(-12.0f); + SetAirDrag(0.2f); } diff --git a/src/Entities/TNTEntity.cpp b/src/Entities/TNTEntity.cpp index a89d2f300..b5b98f833 100644 --- a/src/Entities/TNTEntity.cpp +++ b/src/Entities/TNTEntity.cpp @@ -12,6 +12,8 @@ cTNTEntity::cTNTEntity(double a_X, double a_Y, double a_Z, int a_FuseTicks) : super(etTNT, a_X, a_Y, a_Z, 0.98, 0.98), m_FuseTicks(a_FuseTicks) { + SetGravity(-16.0f); + SetAirDrag(0.4f); } @@ -22,6 +24,8 @@ cTNTEntity::cTNTEntity(const Vector3d & a_Pos, int a_FuseTicks) : super(etTNT, a_Pos.x, a_Pos.y, a_Pos.z, 0.98, 0.98), m_FuseTicks(a_FuseTicks) { + SetGravity(-16.0f); + SetAirDrag(0.4f); } diff --git a/src/Entities/WitherSkullEntity.cpp b/src/Entities/WitherSkullEntity.cpp index a7e774bba..dc95e3edd 100644 --- a/src/Entities/WitherSkullEntity.cpp +++ b/src/Entities/WitherSkullEntity.cpp @@ -16,6 +16,8 @@ cWitherSkullEntity::cWitherSkullEntity(cEntity * a_Creator, double a_X, double a super(pkWitherSkull, a_Creator, a_X, a_Y, a_Z, 0.25, 0.25) { SetSpeed(a_Speed); + SetGravity(0); + SetAirDrag(0); } diff --git a/src/Mobs/Blaze.cpp b/src/Mobs/Blaze.cpp index 89eeb3709..ed2a98201 100644 --- a/src/Mobs/Blaze.cpp +++ b/src/Mobs/Blaze.cpp @@ -11,6 +11,8 @@ cBlaze::cBlaze(void) : super("Blaze", mtBlaze, "mob.blaze.hit", "mob.blaze.death", 0.6, 1.8) { + SetGravity(-8.0f); + SetAirDrag(0.8f); } -- cgit v1.2.3 From d315534b76f0523d0a7bd7973ab7d209ee7d610e Mon Sep 17 00:00:00 2001 From: DevToaster Date: Mon, 30 Mar 2015 20:07:19 -0400 Subject: Adjusted projectile physics --- src/Entities/ProjectileEntity.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 2b8573d4d..28f7d0dea 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -353,9 +353,11 @@ void cProjectileEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a return; } - const Vector3d PerTickSpeed = GetSpeed() / 20; + auto DtSec = std::chrono::duration_cast>(a_Dt); + + const Vector3d DeltaSpeed = GetSpeed() * DtSec.count(); const Vector3d Pos = GetPosition(); - const Vector3d NextPos = Pos + PerTickSpeed; + const Vector3d NextPos = Pos + DeltaSpeed; // Test for entity collisions: cProjectileEntityCollisionCallback EntityCollisionCallback(this, Pos, NextPos); @@ -392,8 +394,8 @@ void cProjectileEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a // Add slowdown and gravity effect to the speed: Vector3d NewSpeed(GetSpeed()); - NewSpeed.y += m_Gravity / 20; - NewSpeed *= TracerCallback.GetSlowdownCoeff(); + NewSpeed.y += m_Gravity * DtSec.count(); + NewSpeed -= NewSpeed * m_AirDrag * DtSec.count(); SetSpeed(NewSpeed); SetYawFromSpeed(); SetPitchFromSpeed(); -- cgit v1.2.3 From 45c84ea93318a4ab8921a20d7d2c086cfc313999 Mon Sep 17 00:00:00 2001 From: DevToaster Date: Tue, 31 Mar 2015 11:03:35 -0400 Subject: Changed air drag units to 'interpolated ticks' per second --- src/Entities/ArrowEntity.cpp | 2 +- src/Entities/Boat.cpp | 2 +- src/Entities/Entity.cpp | 4 ++-- src/Entities/Entity.h | 6 ++++-- src/Entities/FallingBlock.cpp | 2 +- src/Entities/Minecart.cpp | 2 +- src/Entities/Pawn.cpp | 2 +- src/Entities/ProjectileEntity.cpp | 6 +++--- src/Entities/TNTEntity.cpp | 2 +- src/Mobs/Bat.cpp | 2 ++ src/Mobs/Blaze.cpp | 2 +- 11 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Entities/ArrowEntity.cpp b/src/Entities/ArrowEntity.cpp index 3516b7540..32952100c 100644 --- a/src/Entities/ArrowEntity.cpp +++ b/src/Entities/ArrowEntity.cpp @@ -51,7 +51,7 @@ cArrowEntity::cArrowEntity(cPlayer & a_Player, double a_Force) : m_PickupState = psInCreative; } SetGravity(-20.0f); - SetAirDrag(0.2f); + SetAirDrag(0.01f); } diff --git a/src/Entities/Boat.cpp b/src/Entities/Boat.cpp index 8899921ab..4ad418be4 100644 --- a/src/Entities/Boat.cpp +++ b/src/Entities/Boat.cpp @@ -18,7 +18,7 @@ cBoat::cBoat(double a_X, double a_Y, double a_Z) : { SetMass(20.0f); SetGravity(-16.0f); - SetAirDrag(1.0f); + SetAirDrag(0.05f); SetMaxHealth(6); SetHealth(6); } diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 67dae44b2..51cee248e 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -36,7 +36,7 @@ cEntity::cEntity(eEntityType a_EntityType, double a_X, double a_Y, double a_Z, d m_bHasSentNoSpeed(true), m_bOnGround(false), m_Gravity(-9.81f), - m_AirDrag(0.4f), + m_AirDrag(0.02f), m_LastPos(a_X, a_Y, a_Z), m_IsInitialized(false), m_WorldTravellingFrom(nullptr), @@ -944,7 +944,7 @@ void cEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { // Normal gravity fallspeed = m_Gravity * DtSec.count(); - NextSpeed -= NextSpeed * m_AirDrag * DtSec.count(); + NextSpeed -= NextSpeed * (m_AirDrag * 20.0f) * DtSec.count(); } NextSpeed.y += static_cast(fallspeed); } diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index 2c994c550..dd6190ced 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -508,8 +508,10 @@ protected: For realistic effects, this should be negative. For spaaaaaaace, this can be zero or even positive */ float m_Gravity; - /** Stores the air drag that is applied to the entity every tick, measured in speed ratio per second - Acts as air friction and slows down flight */ + /** Stores the air drag that is applied to the entity every tick, measured in speed ratio per tick + Acts as air friction and slows down flight + Will be interpolated if the server tick rate varies + Data: http://minecraft.gamepedia.com/Entity#Motion_of_entities */ float m_AirDrag; /** Last position sent to client via the Relative Move or Teleport packets (not Velocity) diff --git a/src/Entities/FallingBlock.cpp b/src/Entities/FallingBlock.cpp index 82daeb4bf..4a165909a 100644 --- a/src/Entities/FallingBlock.cpp +++ b/src/Entities/FallingBlock.cpp @@ -17,7 +17,7 @@ cFallingBlock::cFallingBlock(const Vector3i & a_BlockPosition, BLOCKTYPE a_Block m_OriginalPosition(a_BlockPosition) { SetGravity(-16.0f); - SetAirDrag(0.4f); + SetAirDrag(0.02f); } diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index 7fdb39ea7..3d56570ba 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -94,7 +94,7 @@ cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) : { SetMass(20.0f); SetGravity(-16.0f); - SetAirDrag(1.0f); + SetAirDrag(0.05f); SetMaxHealth(6); SetHealth(6); SetWidth(1); diff --git a/src/Entities/Pawn.cpp b/src/Entities/Pawn.cpp index a4a9c6f21..fcb686e28 100644 --- a/src/Entities/Pawn.cpp +++ b/src/Entities/Pawn.cpp @@ -14,7 +14,7 @@ cPawn::cPawn(eEntityType a_EntityType, double a_Width, double a_Height) : , m_EntityEffects(tEffectMap()) { SetGravity(-32.0f); - SetAirDrag(0.4f); + SetAirDrag(0.02f); } diff --git a/src/Entities/ProjectileEntity.cpp b/src/Entities/ProjectileEntity.cpp index 28f7d0dea..05b7669cd 100644 --- a/src/Entities/ProjectileEntity.cpp +++ b/src/Entities/ProjectileEntity.cpp @@ -228,7 +228,7 @@ cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, double a m_IsInGround(false) { SetGravity(-12.0f); - SetAirDrag(0.2f); + SetAirDrag(0.01f); } @@ -245,7 +245,7 @@ cProjectileEntity::cProjectileEntity(eKind a_Kind, cEntity * a_Creator, const Ve SetYawFromSpeed(); SetPitchFromSpeed(); SetGravity(-12.0f); - SetAirDrag(0.2f); + SetAirDrag(0.01f); } @@ -395,7 +395,7 @@ void cProjectileEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a // Add slowdown and gravity effect to the speed: Vector3d NewSpeed(GetSpeed()); NewSpeed.y += m_Gravity * DtSec.count(); - NewSpeed -= NewSpeed * m_AirDrag * DtSec.count(); + NewSpeed -= NewSpeed * (m_AirDrag * 20.0f) * DtSec.count(); SetSpeed(NewSpeed); SetYawFromSpeed(); SetPitchFromSpeed(); diff --git a/src/Entities/TNTEntity.cpp b/src/Entities/TNTEntity.cpp index b5b98f833..d849bd4c9 100644 --- a/src/Entities/TNTEntity.cpp +++ b/src/Entities/TNTEntity.cpp @@ -13,7 +13,7 @@ cTNTEntity::cTNTEntity(double a_X, double a_Y, double a_Z, int a_FuseTicks) : m_FuseTicks(a_FuseTicks) { SetGravity(-16.0f); - SetAirDrag(0.4f); + SetAirDrag(0.02f); } diff --git a/src/Mobs/Bat.cpp b/src/Mobs/Bat.cpp index c072d4f48..e187e928a 100644 --- a/src/Mobs/Bat.cpp +++ b/src/Mobs/Bat.cpp @@ -9,6 +9,8 @@ cBat::cBat(void) : super("Bat", mtBat, "mob.bat.hurt", "mob.bat.death", 0.5, 0.9) { + SetGravity(-2.0f); + SetAirDrag(0.05f); } diff --git a/src/Mobs/Blaze.cpp b/src/Mobs/Blaze.cpp index ed2a98201..d4ad24166 100644 --- a/src/Mobs/Blaze.cpp +++ b/src/Mobs/Blaze.cpp @@ -12,7 +12,7 @@ cBlaze::cBlaze(void) : super("Blaze", mtBlaze, "mob.blaze.hit", "mob.blaze.death", 0.6, 1.8) { SetGravity(-8.0f); - SetAirDrag(0.8f); + SetAirDrag(0.05f); } -- cgit v1.2.3 From a9583149b4049249bab66bd4115566f0ea86608c Mon Sep 17 00:00:00 2001 From: DevToaster Date: Tue, 31 Mar 2015 11:40:31 -0400 Subject: Changed air drag for pickups --- src/Entities/Pickup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Entities/Pickup.cpp b/src/Entities/Pickup.cpp index cec186b52..f2f76dbf9 100644 --- a/src/Entities/Pickup.cpp +++ b/src/Entities/Pickup.cpp @@ -92,7 +92,7 @@ cPickup::cPickup(double a_PosX, double a_PosY, double a_PosZ, const cItem & a_It , m_bIsPlayerCreated(IsPlayerCreated) { SetGravity(-16.0f); - SetAirDrag(0.4f); + SetAirDrag(0.02f); SetMaxHealth(5); SetHealth(5); SetSpeed(a_SpeedX, a_SpeedY, a_SpeedZ); -- cgit v1.2.3