From f24412ed630610be2171892eea838be16f056ec6 Mon Sep 17 00:00:00 2001 From: krystalgamer Date: Wed, 7 Aug 2024 20:09:57 +0200 Subject: [PATCH] Utils_TurnTowards --- tips.txt | 3 +++ utils.cpp | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tips.txt b/tips.txt index e500fb3..4bbad98 100644 --- a/tips.txt +++ b/tips.txt @@ -67,3 +67,6 @@ if (x == -1){ worse code - classes/structures with other inner structures/arrays will have weird indexing (i.e SEntry in CMenu) + + +using structures/classes can change code gen - check Utils_turnsTowards, it was using eax,cx on the += and -= statements with i16 but when replace with CSVector it fixed it. diff --git a/utils.cpp b/utils.cpp index 9e813d4..0482870 100644 --- a/utils.cpp +++ b/utils.cpp @@ -460,10 +460,40 @@ void Utils_VblankProcessing(void) printf("Utils_VblankProcessing(void)"); } -// @SMALLTODO -void Utils_TurnTowards(CSVector, CSVector*, CSVector*, CSVector, i32) +// @Ok +// @Matching +void Utils_TurnTowards( + CSVector Current, + CSVector *AngVel, + CSVector *AngAcc, + CSVector Ideal, + i32 accfactor) { - printf("void Utils_TurnTowards(CSVector, CSVector*, CSVector*, CSVector, i32)"); + + CSVector angDiff; + angDiff.vx = Ideal.vx - Current.vx; + angDiff.vy = Ideal.vy - Current.vy; + + if ( angDiff.vx < -2048 ) + angDiff.vx += 4096; + if ( angDiff.vx > 2048 ) + angDiff.vx -= 4096; + + if ( angDiff.vy < -2048 ) + angDiff.vy += 4096; + if ( angDiff.vy > 2048 ) + angDiff.vy -= 4096; + + if ( angDiff.vx || angDiff.vy ) + { + AngAcc->vx = (accfactor * angDiff.vx) >> 8; + AngAcc->vy = (accfactor * angDiff.vy) >> 8; + } + else + { + AngVel->vx = 0; + AngVel->vy = 0; + } } // @NotOk