Discuss Scratch

THECRABGOD
Scratcher
22 posts

Pen collision

I'm making a pen game on scratch and I don't know how to add collision…
MathPuppy314
Scratcher
500+ posts

Pen collision

What exactly are you trying to get collision with?
Lines?
Shapes?
Drawings?
THECRABGOD
Scratcher
22 posts

Pen collision

MathPuppy314 wrote:

What exactly are you trying to get collision with?
Lines?
Shapes?
Drawings?
The walls and coins.
It's not a platformer, too.
Jman1111
Scratcher
54 posts

Pen collision

You can use the touching color sensing block to detect collision.

<touching color [#b036c9] ?>
<color [#214635] is touching [#820756] ?>

If you don't want to rely on colors and if your walls are rectangular you can use AABB collision. and for circular things such as coins, you can compare distances and the radius of the object to detect collision. See https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection for some collision detection techniques.

Last edited by Jman1111 (Aug. 13, 2020 21:07:56)

MathPuppy314
Scratcher
500+ posts

Pen collision

Jman1111 wrote:

You can use the touching color sensing block to detect collision.

<touching color [#b036c9] ?>
<color [#214635] is touching [#820756] ?>

~snip~
Those blocks will not work for 100% pen. They both use the sprite costume to detect colors.

Last edited by MathPuppy314 (Aug. 14, 2020 00:52:37)

THECRABGOD
Scratcher
22 posts

Pen collision

MathPuppy314 wrote:

Jman1111 wrote:

You can use the touching color sensing block to detect collision.

<touching color [#b036c9] ?>
<color [#214635] is touching [#820756] ?>

~snip~
Those blocks will not work for 100% pen. They both use the sprite costume to detect colors.
So then hooooowwwww
ggenije
Scratcher
500+ posts

Pen collision

The easiest way is to use AABB vs AABB collision (AABB means Axis Aligned Bounding Box which is basically a rectangle)

So when you draw a wall it must be axis aligned, and then represent player as AABB , now you can check collision between them.

To represent an AABB you'll need MinX , MaxX, MinY, MaxY.

So to check collision between them use

define AABB vs AABB collision  (minX1) (maxX1) (minY1) (maxY1) | (minX2) (maxX2) (minY2) (maxY2) 
set [collision v] to [0]
if <(minX1) < (maxX2)> then
if <(maxX1) > (minX2)> then
if <(minY1) < (maxY2)> then
if <(maxY1) > (minY2)> then
set [collision v] to [1]
end
end
end
end

And to define AABB from wall it's logical I think.
For player it can be:
minX=playerX
maxX=playerX+playerSize
minY=playerY
maxY=playerY+playerSize

Last edited by ggenije (Aug. 14, 2020 14:17:12)

THECRABGOD
Scratcher
22 posts

Pen collision

ggenije wrote:

The easiest way is to use AABB vs AABB collision (AABB means Axis Aligned Bounding Box which is basically a rectangle)

So when you draw a wall it must be axis aligned, and then represent player as AABB , now you can check collision between them.

To represent an AABB you'll need MinX , MaxX, MinY, MaxY.

So to check collision between them use

define AABB vs AABB collision  (minX1) (maxX1) (minY1) (maxY1) | (minX2) (maxX2) (minY2) (maxY2) 
set [collision v] to [0]
if <(minX1) < (maxX2)> then
if <(maxX1) > (minX2)> then
if <(minY1) < (maxY2)> then
if <(maxY1) > (minY2)> then
set [collision v] to [1]
end
end
end
end

And to define AABB from wall it's logical I think.
For player it can be:
minX=playerX
maxX=playerX+playerSize
minY=playerY
maxY=playerY+playerSize

???
ggenije
Scratcher
500+ posts

Pen collision

First tell me how do you draw walls and player?
THECRABGOD
Scratcher
22 posts

Pen collision

Pen…
Player is:
define draw player
pen down


when green flag clicked
forever
clear
draw player
end

Last edited by THECRABGOD (Aug. 14, 2020 14:30:45)

Jman1111
Scratcher
54 posts

Pen collision

If you make the player a sprite you can use the color collision detection as I suggested before with the other things that you have drawn with the pen. But if you want 100% pen to work you will have to calculate the collision on your own using AABB collisions like @ggenije said, Circle collision by comparing radius and distances, or something else depending on the exact shape of your pen objects. If you refer to my previous post I posted a link that shows you those techniques. Rotated and shapes with many vertices a bit more tricky to calculate.

Last edited by Jman1111 (Aug. 14, 2020 16:10:39)

C-A-T-L-O-VE-R-2
Scratcher
1 post

Pen collision

Bro Thats so cool

Powered by DjangoBB