Discuss Scratch

newcoolman
Scratcher
20 posts

how do i make collision? its for a game remake i'm making

i wanna make it so when the sprite touches touches the wall, it stops moving, but when you are not touching the wall, it goes back to normal. i don't wanna use custom blocks for this though.
ItBeJC
Scratcher
1000+ posts

how do i make collision? its for a game remake i'm making

I'm unsure if it's possible to do this, it might be though. If yo don't mind me asking, why don't you want to use custom blocks? Also, two questions:

1) Are you using velocity
2) Is it a top down game or a platformer type game.
newcoolman
Scratcher
20 posts

how do i make collision? its for a game remake i'm making

ItBeJC wrote:

I'm unsure if it's possible to do this, it might be though. If yo don't mind me asking, why don't you want to use custom blocks? Also, two questions:

1) Are you using velocity
2) Is it a top down game or a platformer type game.
its supposed to be an endless game, with 3 sections, also when i used custom blocks it sorta…broke.

Last edited by newcoolman (April 28, 2023 05:28:23)

Arctevious
Scratcher
500+ posts

how do i make collision? its for a game remake i'm making

i have done this before, in my isometric game (45-degree front view) i used what i called “feelers” just as the name implies, they feel for the shape and orientation of the wall, now here is the good part, “feeler resolution”, like that of the board of slidable nails, if you push it against the wall with 3 nails, the shape will be rather vague, now add 3 billion mini slidable nails on to the board and try again! it now depicts a high-resolution of what the wall looks like. the same concept applies to the character. so in order to achieve this, make a shape of the hitbox you are going to use (don't do circles or you'll regret it), then splice it up into at least 8-16 slices (remember the more slices you make, the more detailed the results, but more there will be more to code if you do) now assign each broadside a group of feelers, like 0, 90, 180, 270 for a square. for what i did i used 4 feelers for each side, totaling up to about 16 feelers in total. after assigning the pieces to a group put the groups in lists designated to the feelers in that region. After having done this, when a feeler is touching a wall you record this in the list by removing the angle if it isn't touching and adding the angle if it is. now after you have set up the feelers, make the feelers DO SOMETHING. what the feelers will do is have a contest to see which region has the most feelers activated, the one who wins is the one who repels the wall if both sides are equal then they both win and repel that side. the point of this is to discourage any foreign shapes from disrupting the WHOLE side if it's only touching a tiny bit of it, which is not what we want. now as for repelling, DO NOT BOTHER with moving the player out of the wall with move scripts… that will just give things excuses to break for no reason. instead, just cut off access to the button that makes it go to the opposing side and if you leave it, then the sensors will say (HEY! We aren't being touched let's enable that blocked button again) and thus you have a working collision script!

hope this helps! if you need clarification i will make a dummy project for you to veiw and backpack code.
newcoolman
Scratcher
20 posts

how do i make collision? its for a game remake i'm making

Arctevious wrote:

i have done this before, in my isometric game (45-degree front view) i used what i called “feelers” just as the name implies, they feel for the shape and orientation of the wall, now here is the good part, “feeler resolution”, like that of the board of slidable nails, if you push it against the wall with 3 nails, the shape will be rather vague, now add 3 billion mini slidable nails on to the board and try again! it now depicts a high-resolution of what the wall looks like. the same concept applies to the character. so in order to achieve this, make a shape of the hitbox you are going to use (don't do circles or you'll regret it), then splice it up into at least 8-16 slices (remember the more slices you make, the more detailed the results, but more there will be more to code if you do) now assign each broadside a group of feelers, like 0, 90, 180, 270 for a square. for what i did i used 4 feelers for each side, totaling up to about 16 feelers in total. after assigning the pieces to a group put the groups in lists designated to the feelers in that region. After having done this, when a feeler is touching a wall you record this in the list by removing the angle if it isn't touching and adding the angle if it is. now after you have set up the feelers, make the feelers DO SOMETHING. what the feelers will do is have a contest to see which region has the most feelers activated, the one who wins is the one who repels the wall if both sides are equal then they both win and repel that side. the point of this is to discourage any foreign shapes from disrupting the WHOLE side if it's only touching a tiny bit of it, which is not what we want. now as for repelling, DO NOT BOTHER with moving the player out of the wall with move scripts… that will just give things excuses to break for no reason. instead, just cut off access to the button that makes it go to the opposing side and if you leave it, then the sensors will say (HEY! We aren't being touched let's enable that blocked button again) and thus you have a working collision script!

hope this helps! if you need clarification i will make a dummy project for you to veiw and backpack code.
can you make it? the message seems quite long lol
legendary34678
Scratcher
1000+ posts

how do i make collision? its for a game remake i'm making

Simple collision works like this:

move (10) steps
if <touching [wall v]?> then
move (-10) steps
end

There are, of course, more sophisticated versions out there, but this is the simplest it gets.

Last edited by legendary34678 (April 28, 2023 05:56:45)

newcoolman
Scratcher
20 posts

how do i make collision? its for a game remake i'm making

legendary34678 wrote:

Simple collision works like this:

move (10) steps
if <touching [wall v]?> then
move (-10) steps
end

There are, of course, more sophisticated versions out there, but this is the simplest it gets.
y'know what? give me a sophisticated version
legendary34678
Scratcher
1000+ posts

how do i make collision? its for a game remake i'm making

A sophisticated version would be something like this:

define try move (x) (y)
change x by (x)
if <touching [wall v]?> then
repeat until <not <touching [wall v]?>>
change x by ((-1) * ((x) / ([abs v] of (x))))
end
end
change y by (y)
if <touching [wall v]?> then
repeat until <not <touching [wall v]?>>
change y by ((-1) * ((y) / ([abs v] of (y))))
end
end

when gf clicked
forever
if <key [w v] pressed?> then
try move (0) (10)
end
if <key [a v] pressed?> then
try move (-10) (0)
end
if <key [s v] pressed?> then
try move (0) (-10)
end
if <key [d v] pressed?> then
try move (10) (0)
end
end
ItBeJC
Scratcher
1000+ posts

how do i make collision? its for a game remake i'm making

legendary34678 wrote:

A sophisticated version would be something like this:

define try move (x) (y)
change x by (x)
if <touching [wall v]?> then
repeat until <not <touching [wall v]?>>
change x by ((-1) * ((x) / ([abs v] of (x))))
end
end
change y by (y)
if <touching [wall v]?> then
repeat until <not <touching [wall v]?>>
change y by ((-1) * ((y) / ([abs v] of (y))))
end
end

when gf clicked
forever
if <key [w v] pressed?> then
try move (0) (10)
end
if <key [a v] pressed?> then
try move (-10) (0)
end
if <key [s v] pressed?> then
try move (0) (-10)
end
if <key [d v] pressed?> then
try move (10) (0)
end
end
And of course, with smoothness:
when green flag clicked
forever
change [xvelocity v] by (<<key [right arrow v] pressed?> or <key [d v] pressed?>> - <<key [left arrow v] pressed?> or <key [a v] pressed?>>)
change [yvelocity v] by (<<key [up arrow v] pressed?> or <key [w v] pressed?>> - <<key [down arrow v] pressed?> or <key [s v] pressed?>>)
set [xvelocity v] to ((xvelocity) * (0.85))
set [yvelocity v] to ((yvelocity) * (0.85))
Try Move (xvelocity) (yvelocity) :: custom
end
define Try Move (x) (y)
change x by (x)
if <touching [Wall v] ?> then
repeat until <not <touching [wall v] ?>>
change x by ((-1) * ((x) / ([abs v] of (x))))
end
set [xvelocity v] to [0]
end
change y by (y)
if <touching [Wall v] ?> then
repeat until <not <touching [wall v] ?>>
change y by ((-1) * ((y) / ([abs v] of (y))))
end
set [yvelocity v] to [0]
Making the block run without screen refresh and preferably making the variables “for this sprite only.”

Last edited by ItBeJC (April 28, 2023 15:20:08)

Arctevious
Scratcher
500+ posts

how do i make collision? its for a game remake i'm making

newcoolman wrote:

Arctevious wrote:

i have done this before, in my isometric game (45-degree front view) i used what i called “feelers” just as the name implies, they feel for the shape and orientation of the wall, now here is the good part, “feeler resolution”, like that of the board of slidable nails, if you push it against the wall with 3 nails, the shape will be rather vague, now add 3 billion mini slidable nails on to the board and try again! it now depicts a high-resolution of what the wall looks like. the same concept applies to the character. so in order to achieve this, make a shape of the hitbox you are going to use (don't do circles or you'll regret it), then splice it up into at least 8-16 slices (remember the more slices you make, the more detailed the results, but more there will be more to code if you do) now assign each broadside a group of feelers, like 0, 90, 180, 270 for a square. for what i did i used 4 feelers for each side, totaling up to about 16 feelers in total. after assigning the pieces to a group put the groups in lists designated to the feelers in that region. After having done this, when a feeler is touching a wall you record this in the list by removing the angle if it isn't touching and adding the angle if it is. now after you have set up the feelers, make the feelers DO SOMETHING. what the feelers will do is have a contest to see which region has the most feelers activated, the one who wins is the one who repels the wall if both sides are equal then they both win and repel that side. the point of this is to discourage any foreign shapes from disrupting the WHOLE side if it's only touching a tiny bit of it, which is not what we want. now as for repelling, DO NOT BOTHER with moving the player out of the wall with move scripts… that will just give things excuses to break for no reason. instead, just cut off access to the button that makes it go to the opposing side and if you leave it, then the sensors will say (HEY! We aren't being touched let's enable that blocked button again) and thus you have a working collision script!

hope this helps! if you need clarification i will make a dummy project for you to veiw and backpack code.
can you make it? the message seems quite long lol
SURE!!! here yee go… https://scratch.mit.edu/projects/842949896/ i warn you by the way to not use the others code if you want peculiar strange shapes in the game, their code only really works for squares and will bug the hell out if touched by a circle or decrescendoed wall ( a sidways V) though while my code works for all shapes it is not the best for speed collision, which they have the benefit on that over me in code, the faster your charactor is the worse the collision detection is because the player gets inside the wall faster than the detection can tick, so id say max speed should be 5-10, anything over and you could skip objects. but their scripts will prevent that but will give a worse over-all collision detection in general.

Last edited by Arctevious (April 28, 2023 18:52:15)

benji40085430
Scratcher
2 posts

how do i make collision? its for a game remake i'm making

ngl I'm bad at coding and am just trying to create a collision for pillars to jump on, is there any easy way to do that
helloimnob
Scratcher
100+ posts

how do i make collision? its for a game remake i'm making

benji40085430 wrote:

ngl I'm bad at coding and am just trying to create a collision for pillars to jump on, is there any easy way to do that
 // assuming you already have your walking system
when green flag clicked
forever
if <<touching [wall v] ?> and <key [d or left arrow v] pressed?>> then // detects if your character sprite is touching a wall, the map needs to be a sprite for touching wall.
change x by (-5)
end
end
this is the simplest i know

Last edited by helloimnob (Aug. 31, 2025 13:49:26)

cosmosaura
Scratch Team
1000+ posts

how do i make collision? its for a game remake i'm making

Topic closed due to necroposting.

Powered by DjangoBB