Skip to content

Use fixed point math for position, endurance, health - #1044

Draft
Nopey wants to merge 12 commits into
masterfrom
fixed-point
Draft

Use fixed point math for position, endurance, health#1044
Nopey wants to merge 12 commits into
masterfrom
fixed-point

Conversation

@Nopey

@Nopey Nopey commented Mar 26, 2025

Copy link
Copy Markdown
Member

This PR hopes to improve netplay and rec compatibility & determinism across platforms.

Positions and velocities (vec2f) are now stored as 24.8 fixed point numbers, which means there are 24 bits for the whole part and 8 for the fractional part.

This PR has the annoying quirk of taking an existing name and appending 'f' to mark it as fixedpoint, some of these should be un-renamed before merging-- this was done to ensure non-updated code would compiler error while writing the PR.

Comment thread src/game/protos/object.h
Comment thread src/game/protos/object.c
Comment thread src/game/protos/player.c
Comment thread src/utils/vec.h Outdated
@katajakasa

This comment was marked as resolved.

@LegendaryBlueShirt

This comment was marked as resolved.

@katajakasa

This comment was marked as resolved.

Comment thread src/formats/af.h Outdated
Comment thread src/game/objects/har.h Outdated
@Nopey

Nopey commented Mar 27, 2025

Copy link
Copy Markdown
Member Author

Will rebase onto latest master once #1054 is merged. EDIT: rebased.

@Nopey
Nopey force-pushed the fixed-point branch 2 times, most recently from 8d15229 to d8c7b6d Compare March 29, 2025 03:38
Nopey and others added 6 commits March 28, 2025 20:52
)

* Try to improve the fixedpt_str function and add unit tests for it

* Fix overflow in fixedpt_fracpart macro

* Futher clarify the function

* Clarify fixed point string printing test suite name

* Clearer handling of negative numbers

* Apply suggestions from code review

---------

Co-authored-by: Magnus Larsen <golgothasTerror101@gmail.com>
@Nopey
Nopey marked this pull request as ready for review March 29, 2025 03:56
@Nopey Nopey added this to the 0.8.1 milestone Mar 29, 2025
Comment thread NOTICE Outdated
@@ -0,0 +1,25 @@
This software contains fixedptc, whose license follows:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be combined with the LICENSE file?

#define ARENA_RIGHT_WALL 300
#define ARENA_FLOOR 190

// fixed point versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably we should remove the non fixed point versions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARENA_FLOOR is important for object rendering, and we render in pixels-- not fixedpt fractions of pixels.
The arena walls can be fixedpt only, sure.

Comment thread src/game/protos/object.h
Comment thread src/utils/random.c Outdated
}
fixedpt rand_fixedpt(fixedpt max) {
return random_fixedpt(&rand_state, max);
} No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a newline at end of this file

Comment thread tools/aftool/main.c
printf("%d\n", af->health);
break;
case 5:
printf("%f\n", af->forward_speed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably we could print these as fixedpt_strs

Comment thread src/game/scenes/arena.c Outdated
if(info) { // Only Power Plant has the electric overlay effect
object *obj2 = omf_calloc(1, sizeof(object));
object_create(obj2, scene->gs, vec2i_create(o_har->pos.x, o_har->pos.y), vec2f_create(0, 0));
object_create(obj2, scene->gs, vec2f_to_i(o_har->pos), vec2f_createf(0, 0));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we should create objects in the fixed point space?

@Nopey
Nopey marked this pull request as draft September 14, 2025 19:47
@LegendaryBlueShirt LegendaryBlueShirt added the accuracy Relating to the accuracy of the game to the original label Aug 29, 2026
Comment thread src/game/gui/trn_menu.c

static vec2f center(component *c) {
return vec2f_create(c->x + c->w / 2, c->y + c->h / 2);
return vec2f_create(fixedpt_fromint(c->x + c->w) / 2, fixedpt_fromint(c->y + c->h) / 2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the math is changing here due to order of operations

@LegendaryBlueShirt

Copy link
Copy Markdown
Contributor

Would it make sense//Is it possible to split this up into multiple PRs? We can start with one PR for the fixed point library at least

@Nopey

Nopey commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

Fixed-point health, damage, endurance is a good candidate for being split off to a separate PR; these do not use the 24.8 fixed point format that the on-screen positions use (24.8 implemented here in fixedptc.h).

When reviewing a PR like this, I suggest using the Commits tab of GitHub, as opposed to the Changed Files tab; I split the work across a dozen commits for readability and reviewability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accuracy Relating to the accuracy of the game to the original

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants