From 43e2ab896b90ef09526080790b71ea337e0083b5 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Mon, 13 Mar 2023 10:54:33 +0300 Subject: [PATCH] FT collision part6: fixes and cleanup. --- SpaceCadetPinball/TBall.cpp | 1 - SpaceCadetPinball/TBall.h | 1 - SpaceCadetPinball/TDemo.cpp | 2 +- SpaceCadetPinball/TPinballTable.cpp | 1 - SpaceCadetPinball/maths.cpp | 4 ++-- SpaceCadetPinball/maths.h | 1 - SpaceCadetPinball/pb.cpp | 26 +++++++++++--------------- SpaceCadetPinball/pb.h | 2 +- 8 files changed, 15 insertions(+), 23 deletions(-) diff --git a/SpaceCadetPinball/TBall.cpp b/SpaceCadetPinball/TBall.cpp index bfe6750..0445b7b 100644 --- a/SpaceCadetPinball/TBall.cpp +++ b/SpaceCadetPinball/TBall.cpp @@ -17,7 +17,6 @@ TBall::TBall(TPinballTable* table, int groupIndex) : TCollisionComponent(table, visualStruct visual{}; char ballGroupName[10]{"ball"}; - TimeNow = 0.0; RayMaxDistance = 0.0; ActiveFlag = 1; CollisionComp = nullptr; diff --git a/SpaceCadetPinball/TBall.h b/SpaceCadetPinball/TBall.h index e15f213..79f4174 100644 --- a/SpaceCadetPinball/TBall.h +++ b/SpaceCadetPinball/TBall.h @@ -24,7 +24,6 @@ public : float Speed; float RayMaxDistance; float TimeDelta; - float TimeNow; vector2 RampFieldForce{}; TCollisionComponent* CollisionComp; int CollisionMask; diff --git a/SpaceCadetPinball/TDemo.cpp b/SpaceCadetPinball/TDemo.cpp index 807673c..42b7632 100644 --- a/SpaceCadetPinball/TDemo.cpp +++ b/SpaceCadetPinball/TDemo.cpp @@ -125,7 +125,7 @@ void TDemo::Collision(TBall* ball, vector2* nextPosition, vector2* direction, fl case 1404: if (!PlungerFlag) { - PinballTable->Message(MessageCode::PlungerInputPressed, ball->TimeNow); + PinballTable->Message(MessageCode::PlungerInputPressed, 0); float time = RandFloat() + 2.0f; PlungerFlag = timer::set(time, this, PlungerRelease); } diff --git a/SpaceCadetPinball/TPinballTable.cpp b/SpaceCadetPinball/TPinballTable.cpp index f82d6ef..0e1bcdb 100644 --- a/SpaceCadetPinball/TPinballTable.cpp +++ b/SpaceCadetPinball/TPinballTable.cpp @@ -615,7 +615,6 @@ TBall* TPinballTable::AddBall(vector2 position) ball->Direction = {}; ball->Speed = 0; ball->TimeDelta = 0; - ball->TimeNow = 0; ball->EdgeCollisionCount = 0; ball->CollisionFlag = 0; ball->CollisionMask = 1; diff --git a/SpaceCadetPinball/maths.cpp b/SpaceCadetPinball/maths.cpp index 2c3107c..1efd066 100644 --- a/SpaceCadetPinball/maths.cpp +++ b/SpaceCadetPinball/maths.cpp @@ -251,8 +251,8 @@ vector2 maths::vector_mul(const vector2& vec1, float val) float maths::basic_collision(TBall* ball, vector2* nextPosition, vector2* direction, float elasticity, float smoothness, float threshold, float boost) { - ball->Position.X = nextPosition->X; - ball->Position.Y = nextPosition->Y; + ball->Position.X = nextPosition->X + direction->X * 0.0005f; + ball->Position.Y = nextPosition->Y + direction->Y * 0.0005f; // Project ball direction on collision rebound direction auto reboundProj = -DotProduct(*direction, ball->Direction); diff --git a/SpaceCadetPinball/maths.h b/SpaceCadetPinball/maths.h index 420777b..9d0368c 100644 --- a/SpaceCadetPinball/maths.h +++ b/SpaceCadetPinball/maths.h @@ -52,7 +52,6 @@ struct ray_type vector2 Direction; float MaxDistance; float MinDistance; - float TimeNow; int CollisionMask; }; diff --git a/SpaceCadetPinball/pb.cpp b/SpaceCadetPinball/pb.cpp index b09ac34..97f1c14 100644 --- a/SpaceCadetPinball/pb.cpp +++ b/SpaceCadetPinball/pb.cpp @@ -285,7 +285,7 @@ void pb::frame(float dtMilliSec) float dtSec = dtMilliSec * 0.001f; time_next = time_now + dtSec; - timed_frame(time_now, dtSec, true); + timed_frame(dtSec); time_now = time_next; dtMilliSec += time_ticks_remainder; @@ -318,7 +318,7 @@ void pb::frame(float dtMilliSec) } } -void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) +void pb::timed_frame(float timeDelta) { for (auto ball : MainTable->BallList) { @@ -346,12 +346,13 @@ void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) } } - int ballSteps[20]{-1}; + int ballSteps[20]{}; float ballStepsDistance[20]{}; int maxStep = -1; for (auto index = 0u; index < MainTable->BallList.size(); index++) { auto ball = MainTable->BallList[index]; + ballSteps[index] = -1; if (ball->ActiveFlag != 0) { vector2 vecDst{}; @@ -366,8 +367,8 @@ void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) else { TTableLayer::edge_manager->FieldEffects(ball, &vecDst); - vecDst.X *= timeDelta; - vecDst.Y *= timeDelta; + vecDst.X *= ball->TimeDelta; + vecDst.Y *= ball->TimeDelta; ball->Direction.X *= ball->Speed; ball->Direction.Y *= ball->Speed; maths::vector_add(ball->Direction, vecDst); @@ -401,16 +402,15 @@ void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) for (auto ballIndex = 0u; ballIndex < MainTable->BallList.size(); ballIndex++) { auto ball = MainTable->BallList[ballIndex]; - if (!ball->CollisionDisabledFlag && step <= ballSteps[ballIndex]) + if (!ball->CollisionDisabledFlag && ballSteps[ballIndex] >= step) { ray.CollisionMask = ball->CollisionMask; - ball->TimeNow = timeNow; for (auto distanceSum = 0.0f; distanceSum < BallHalfRadius;) { ray.Origin = ball->Position; ray.Direction = ball->Direction; - if (step >= ballSteps[ballIndex]) + if (ballSteps[ballIndex] <= step) { ray.MaxDistance = ballStepsDistance[ballIndex] - ballSteps[ballIndex] * BallHalfRadius; } @@ -418,7 +418,6 @@ void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) { ray.MaxDistance = BallHalfRadius; } - ray.TimeNow = ball->TimeNow; TEdgeSegment* edge = nullptr; auto distance = TTableLayer::edge_manager->FindCollisionDistance(&ray, ball, &edge); @@ -462,13 +461,10 @@ void pb::timed_frame(float timeNow, float timeDelta, bool drawBalls) flipper->UpdateSprite(); } - if (drawBalls) + for (auto ball : MainTable->BallList) { - for (auto ball : MainTable->BallList) - { - if (ball->ActiveFlag) - ball->Repaint(); - } + if (ball->ActiveFlag) + ball->Repaint(); } } diff --git a/SpaceCadetPinball/pb.h b/SpaceCadetPinball/pb.h index 2a5866c..7b951ea 100644 --- a/SpaceCadetPinball/pb.h +++ b/SpaceCadetPinball/pb.h @@ -66,7 +66,7 @@ public: static void replay_level(bool demoMode); static void ballset(float dx, float dy); static void frame(float dtMilliSec); - static void timed_frame(float timeNow, float timeDelta, bool drawBalls); + static void timed_frame(float timeDelta); static void pause_continue(); static void loose_focus(); static void InputUp(GameInput input);