0

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?

cbs
  • 5
  • 2
  • Why does your logic for platform intersection compare `y` with `width`? – Jmb Jul 31 '21 at 07:18
  • it does seem to be pointless that. Originally i followed a tutorial for making a pong game from the internet and basically repurposed the intersect routine for this game, probably without really understanding what is going on. Now you point it out it does seem useless to do that and i removed it. Thanks! – cbs Jul 31 '21 at 11:25
  • Probably not useless, but it should compare `y` with *`height`*, not `width`. – Jmb Jul 31 '21 at 11:39

1 Answers1

0

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).

Jmb
  • 18,893
  • 2
  • 28
  • 55
  • Hi @Jmb thanks for your comment. I did multiple times to use on line 346 e.g. let platform_rect = graphics::Rect::new( platform.platform_pos.x, platform.platform_pos.y, platform.width, PLATFORM_HEIGHT, ); But it just doesn't draw the rectangle. Am I missing something with that? – cbs Jul 31 '21 at 09:35
  • I think it should be `Rect::new (-platform.width * 0.5, -PLATFORM_HEIGHT_HALF, platform.width, PLATFORM_HEIGHT);` since you specify the `x`/`y` position later on [line 359](https://github.com/country-bumpkin-software/rusty-miner/blob/main/src/main.rs#L359). – Jmb Jul 31 '21 at 10:05
  • Ahhh @Jmb thanks a lot that works!!! It. was driving me crazy. I still don't quite get why the rect func x, y coords work like that, being negative and all – cbs Jul 31 '21 at 11:01
  • I guess the rect func is based on the centre pos of the rec and it some how works out the x, y from this centre position hence the negative number times the half dimensions. I thought of that last week but it probably didn't sink in enough - obviously. – cbs Jul 31 '21 at 11:03