Discuss Scratch

CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

Link to relevant project: https://scratch.mit.edu/projects/555194353

Hello all!

In my project, I've got a collision system working for the player character (sprite: player_char). The hitbox of player_char is determined by the movement of the rectangle sprite “player,” which has ghost effect moved up to 100% and is therefore invisible when playing the game.

The issue is that, whenever there is the corner in the environment, the collisions system (defined by the function player.movement in the player sprite) lags out/freezes. This occurs on Turbowarp.org as well, and can be tested by jumping or sliding to corners (use WASD or arrow keys to move).

How can I fix this? Thanks in advance!

Some notes about the scripting:
- player.movement has “Run without screen refresh on.”
- x-p stands for x-position, and y-p stands for y-position
- x-v stands for x-velocity, and y-v stands for y-velocity

I hope that all other naming conventions are self-explanatory, but please feel free to ask for any clarification about my scripting!
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

BTW, could the scrolling mechanism be the thing messing with this? I don't see how, but it's not out of the range of possibility.
donotforgetmycode
Scratcher
1000+ posts

Scrolling Platformer Collision Problem

In the player.movement custom block:

...
camera.snap_to_pos :: custom
if <touching [environment v]?> then
camera.update_pos :: custom // Add this block!
repeat until <not <touching [environment v]?>>
...
end
...
end
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

Hi @donotforgetmycode,

Thanks for the suggestion!

Unfortunately, it didn't fix the problem. To be clearer, the lag issue occurs when I slide from left to right into the corner on the right side of the bottom platform.

I haven't been able to debug the problem exactly, either. Any ideas?
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

It also occurs when I jump up against the wall on the right side and slide down while pressing down the right arrow key, so I think the issue stems from the horizontal collision. I'm not still completely sure exactly what the issue is, however.
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

Fixed it! Seems like it had to do with the scrolling.
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

Actually, it isn't quite fixed – the solution I've found messes up the gameplay.

It's very simple, but makes no sense whatsoever to me.

if <touching [environment v]?> then
...
repeat until <not <touching [environment v]>>
say ((-1)*((abs(player.x-v))/(player.x-v))) // (this is the new block that I introduced to fix the problem, originally as a debugging tool)
change [player.x-p v] by ((-1)*((abs(player.x-v))/(player.x-v))) // (i'm still new to formatting scratch blocks, plz ignore the red absolute value func)
...
end
...
end

For some reason, this removes any lag. Why? Is there any substitute to this that does the same thing lag-wise, since I don't want the player moving around saying “Hello” at all times?

Last edited by CodeWizard399 (July 24, 2021 16:23:37)

Thingied
Scratcher
1000+ posts

Scrolling Platformer Collision Problem

Huh, that's kind of strange. I can't get the player to lag out the project at all with and without the say block.
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

Hmm. The movement that's causing it to lag on my side is when, after spawning, I move the player all the way to the right and into the corner. Then I wait there for a second, and it freezes up.

But again, it only occurs without the say block.
Thingied
Scratcher
1000+ posts

Scrolling Platformer Collision Problem

CodeWizard399 wrote:

But again, it only occurs without the say block.
Hmmm, try hiding the text bubble after saying it. I really can't think of anything else because I can't replicate the lag no matter what.
CodeWizard399
Scratcher
44 posts

Scrolling Platformer Collision Problem

That seems to work! Thank you!

Powered by DjangoBB