Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How can I do *accurate* collision detection?
- noahc3
-
14 posts
How can I do *accurate* collision detection?
I would like to make some platformer games and top-down games, however one problem I always have is collision detection. Anything I try to make works, but has tons of exploits and glitches which makes the game more like a cheating fest. Any examples I see that are explained use colors and color detection. It works, but it definitely isn't pretty. I always see other projects that have great collision detection, but I don't understand how the script works. If anyone could explain a good detection system to me (even if its long) it would be much appreciated.
- Programming_Guru
-
84 posts
How can I do *accurate* collision detection?
georgesky had it right, but I think what you're refering to is the glitchless 1 script platforming code:
I believe that is it, I find the best way to find out how a script works is to construct it on your own, now of course this does not support slop gliding and wall jumping, but this is one of Scratch's most flawless platforming engines I've seen,
I hope this helped, if not please come to me with anymore questions that you may have!
I believe that is it, I find the best way to find out how a script works is to construct it on your own, now of course this does not support slop gliding and wall jumping, but this is one of Scratch's most flawless platforming engines I've seen,
I hope this helped, if not please come to me with anymore questions that you may have!
- gtoal
-
1000+ posts
How can I do *accurate* collision detection?
I would like to make some platformer games and top-down games, however one problem I always have is collision detection. Anything I try to make works, but has tons of exploits and glitches which makes the game more like a cheating fest. Any examples I see that are explained use colors and color detection. It works, but it definitely isn't pretty. I always see other projects that have great collision detection, but I don't understand how the script works. If anyone could explain a good detection system to me (even if its long) it would be much appreciated.
Simple collision detect support by Scratch's “touching” blocks works well as long as your sprites move slowly. If you have fast moving sprites it's sometimes possible to have one object move so far past the object that you want to test for collision, that Scratch never sees them touch, and you find your 5-pixel wide man has run through a 1-pixel-wide wall because his last movement was 7 pixels…
The solution to this is unfortunately complex. You need to code your own collision detection rather than using sprite touching, and you have to do things such as calculating the intersection of two vectors to see if your next move crosses the target object's nearest face at any point.
Easiest solution until you've had more experience or are really good at maths, is to limit the speed of your objects so that they never travel through obstacles, and also remember when you do hit an obstacle, you may overlap it by a small number of pixels, and sometimes it's necessary to step backwards by that amount of overlap until you're just at the point where you were about to touch.
G
Last edited by gtoal (April 7, 2015 21:58:13)
- Fusion51
-
1 post
How can I do *accurate* collision detection?
Why cannot you use
This actually doesn't work. I tried it because I'm trying to make a top down game, but my player just stops. No offense though, just saying..
- BKFighter
-
1000+ posts
How can I do *accurate* collision detection?
You would have to make a lot of modifications to that script to get it too work in a platformer, but the basic concept is sound.Why cannot you use
This actually doesn't work. I tried it because I'm trying to make a top down game, but my player just stops. No offense though, just saying..
- ScratchR30
-
6 posts
How can I do *accurate* collision detection?
For the player sprite, do these two scripts:
1. WHEN GREEN FLAG CLICKED
GO TO <location>
FOREVER
IF KEY UP ARROW PRESSED,
(this is a custom block) TryMove (0)
IF KEY DOWN ARROW PRESSED,
(this is the same custom block) TryMove (180)
IF KEY LEFT ARROW PRESSED,
(this is the same custom block) TryMove (-90)
IF KEY RIGHT ARROW PRESSED,
(this is the same custom block) TryMove (90)
2. DEFINE (this is the definition of the custom block) TryMove (direction)
Point in direction (input in the TryMove definition)
Move 4 steps
IF TOUCHING <sprite you want to collide with>,
Move -4 steps
Hopefully this wasn't too confusing, I'm bad at showing with blocks.
1. WHEN GREEN FLAG CLICKED
GO TO <location>
FOREVER
IF KEY UP ARROW PRESSED,
(this is a custom block) TryMove (0)
IF KEY DOWN ARROW PRESSED,
(this is the same custom block) TryMove (180)
IF KEY LEFT ARROW PRESSED,
(this is the same custom block) TryMove (-90)
IF KEY RIGHT ARROW PRESSED,
(this is the same custom block) TryMove (90)
2. DEFINE (this is the definition of the custom block) TryMove (direction)
Point in direction (input in the TryMove definition)
Move 4 steps
IF TOUCHING <sprite you want to collide with>,
Move -4 steps
Hopefully this wasn't too confusing, I'm bad at showing with blocks.
- Tessal65
-
41 posts
How can I do *accurate* collision detection?
The method I often use for collision is one I think is the best. When you move forwards in scratch say 10 steps. and hit a wall 5 steps in. scratch will continue until you have finished all 10 steps. By that point you are deep into the block. Moving backwards exactly 10 steps isn't a bad Idea. But there will still be 5 steps between you and the wall. So you have to move backwards one step until you are no longer touching the wall. But I have a different method inspired by Griffpatch. Always move in tiny steps. But don't refresh the screen until I have moved completely. The way to do this is:
NOTE: Make sure that the custom block has the “run without screen refresh” checkbox enabled!
Now you can use this move in steps custom block to move with proper collision.
NOTE: Make sure that the custom block has the “run without screen refresh” checkbox enabled!
Last edited by Tessal65 (June 27, 2022 16:19:59)
- scratch___user12345
-
1000+ posts
How can I do *accurate* collision detection?
For the player sprite, do these two scripts:
1. WHEN GREEN FLAG CLICKED
GO TO <location>
FOREVER
IF KEY UP ARROW PRESSED,
(this is a custom block) TryMove (0)
IF KEY DOWN ARROW PRESSED,
(this is the same custom block) TryMove (180)
IF KEY LEFT ARROW PRESSED,
(this is the same custom block) TryMove (-90)
IF KEY RIGHT ARROW PRESSED,
(this is the same custom block) TryMove (90)
2. DEFINE (this is the definition of the custom block) TryMove (direction)
Point in direction (input in the TryMove definition)
Move 4 steps
IF TOUCHING <sprite you want to collide with>,
Move -4 steps
Hopefully this wasn't too confusing, I'm bad at showing with blocks.
I know you two are trying to help, but this question is from 7 years ago and it just went up to the top of the page. Look at the date before you post next time The method I often use for collision is one I think is the best. When you move forwards in scratch say 10 steps. and hit a wall 5 steps in. scratch will continue until you have finished all 10 steps. By that point you are deep into the block. Moving backwards exactly 10 steps isn't a bad Idea. But there will still be 5 steps between you and the wall. So you have to move backwards one step until you are no longer touching the wall. But I have a different method inspired by Griffpatch. Always move in tiny steps. But don't refresh the screen until I have moved completely. The way to do this is:Now you can use this move in steps custom block to move with proper collision.
NOTE: Make sure that the custom block has the “run without screen refresh” checkbox enabled!

- ScratchCat_50
-
100+ posts
How can I do *accurate* collision detection?
Because if you touch the collisions sprite then set the speed to zero you cant move to not be touching the collisions sprite Why cannot you use
- Discussion Forums
- » Help with Scripts
-
» How can I do *accurate* collision detection?