Discuss Scratch

mcdowels
Scratcher
17 posts

checking collision between two sprite multiple times per frame, complicated

demonstration project: https://scratch.mit.edu/projects/847508170/

In this game, there are an unfixed number of clone actors which need to sequentially check collisions with another “map” sprite each frame. sometimes multiple times per actor per frame.

the reason is, this is a scrolling game, and I want actors to be able to be active even while their position is outside the current camera window. to achieve this, for an actor collision check the map tile is centered at 0,0. the map costume is set to the current portion of the map which the actor being processed is in. then the actor is positioned relative to 0,0 (the map piece). this represents its current position inside the map piece. collision is then checked. The map sprite is also used for player collisions and has a lot of costumes. I was thinking about combining the actor and map sprites to just compare against themselves, but I'm not sure that would work either.

The problem is, I cant find a way to perform this process within a single frame for all actors and all their collision checks due to broadcasts only being able to run once per frame. I don't know if there is a way around that problem, or another solution to allowing for these types of collisions. Any ideas are appreciated.
supa-panda
Scratcher
100+ posts

checking collision between two sprite multiple times per frame, complicated

I would say: Just do it all with lists. (If you need more help I can give it)
mcdowels
Scratcher
17 posts

checking collision between two sprite multiple times per frame, complicated

supa-panda wrote:

I would say: Just do it all with lists. (If you need more help I can give it)
thats what I was thinking, but do you mean store level data in a list? because even with all actor data only in a list I cant seem to get around this limitation
supa-panda
Scratcher
100+ posts

checking collision between two sprite multiple times per frame, complicated

Hmm tricky. Well, yes, you would have to store all the level information in a list. Some thing like a tile based world.
ggenije
Scratcher
500+ posts

checking collision between two sprite multiple times per frame, complicated

It is impossible to move other objects within one sprite (including clones of same type) within one frame.
Nevertheless there are some solutions, but it yet depends on the size (relative to screen) and structure of your level , so it would be helpful if you provide that as well.
and I want actors to be able to be active even while their position is outside the current camera window. to achieve this
Is it really necessary that actors across whole map must have collisions. That sounds ineffective , because you can't see them anyways so what is the point. I get if they're offscreen but relatively close, then you maybe want, in that case you would want to use first solution down there.

Solutions: (3 different)
  1. Fix level collision sprite to (0,0) and make it small using size block that way it fits into whole level. Now as I said if you don't need to update the whole level just about 50% off the bounds for the example, you make only that part fit in. So when you check collisions for actor you can't easily simulate collisions by modifying their position to zoomed out world. Only problem if you want to check for whole level , but your level is huge, in such case the precision will be unsatisfactory which will lead to “janky” physics.

  2. Next solution is really inelegant. You need to make a new sprite for each level part which is scrolling, then you can put each into (0,0) similarly as previous solution just not with reduced size of level. Then you can calculate which which parts you need collision to check with.

  3. Put level and actor data into lists, for this it's crucial to tell what is structure of your level. It may be possible to store it as tile grid, lines or AABBs (axis aligned bounding boxes). After you put everything in list you can very easy solve collision physics from one sprite in whatever order you want.

Again in conclusion, you can't move one object from another's script, for that reason you're forced either to fix levels to one position while checking collision or to create custom collision scripts (circle vs circle, rectangle vs rectangle, line vs circle etc.)

Also what is the actual point of your project(game) , it is possible you don't even to do the very thing you're asking here to get the desired goal.

Last edited by ggenije (May 9, 2023 07:21:05)

mcdowels
Scratcher
17 posts

checking collision between two sprite multiple times per frame, complicated

but it yet depends on the size (relative to screen) and structure of your level , so it would be helpful if you provide that as well.
The level piece fits within the screen, each area is probably around 5x5 scratch screens.

Im assuming its also impossible to set a sprites costume from a different sprite multiple times per frame?

Also what is the actual point of your project(game) , it is possible you don't even to do the very thing you're asking here to get the desired goal.

The game is a scrolling platformer with combat against the “actors”, which is why I wanted off screen collision, so that they wouldn't freeze and basically cease to exist just because they are outside the cameras view a little bit. I suppose I might have to deal with that though because the level currently is just made of hundreds of images. i've decided against using tiles a while ago which I realize would not have had this problem.
Thanks for helping

Powered by DjangoBB