Struggling with 2d platforms physics

95 Views Asked by At

Im making a game in Rust(Im a beginner) for my son and am somehow struggling with platforms - specifically modifying their length. I can get the platforms if i use certain numbers but if i try to modify the platform width I run into problems.

If i use the const PLATFORM_WIDTH=400 then everything works fine (i seemingly can make any number of them and it all works), but I don't want to have platforms of a fixed width (length), I want each platform to be of variable width. If I try to make the platform width say 100.0 then it is rendered correctly but I can walk over empty space as if there was a platform there. If I make them greater than 400 they are rendered but the sprite will fall through solid floor at some points.

Really don't know where Im going wrong and this annoying issue is preventing me from making progress on my game.

Id really appreciate if someone could take a look at my code and tell me where im going wrong.

My code is here:

https://github.com/country-bumpkin-software/rusty-miner/blob/main/src/main.rs

Also can someone explain to me why the Rect function seems to take negative numbers for the x, y coords?

1

There are 1 best solutions below

4
On BEST ANSWER

The problem is that you sometimes use the width from the platform struct (e.g. when checking for intersections on line 264) and sometimes use the PLATFORM_WIDTH and PLATFORM_WIDTH_HALF constants (e.g. when drawing the platform on line 346).