Discord
Community
DARK THEME

# I made my own collision detection code

I made a collision detection code:

``````init = function()
posdistx = sqrt(mouse.x-0)
posdisty = sqrt(mouse.y-0)
negdistx = sqrt(0-mouse.x)
negdisty = sqrt(0-mouse.y)
end

update = function()
posdistx = sqrt(mouse.x-0)
posdisty = sqrt(mouse.y-0)
negdistx = sqrt(0-mouse.x)
negdisty = sqrt(0-mouse.y)
end

draw = function()
screen.clear("rgb(0,0,0)")
screen.fillRect(0,0,50,20,"rgb(0,255,0")
if negdistx <= 5 then
if posdistx <= 5 then
if negdisty <= 3 then
if posdisty <= 3 then
screen.drawText("hit"0,0,20,"rgb(255,255,255")
end
end
end
end
end
``````

Will I ever use this, maybe.

What i tend to do with collision is just detect if its in between the x and y values of the collision box and then run a bit of code based on that (like if its in this box set x to 67) by making four of the colliders around the square that will set x y or z to a certain value that will cause a box of collision that will reset population.

What i tend to do with collision is just detect if its in between the x and y values of the collision box and then run a bit of code based on that (like if its in this box set x to 67) by making four of the colliders around the square that will set x y or z to a certain value that will cause a box of collision that will reset population.

I noticed a problem with it.

this is the distance equation:

``````dist sqrt{(x_2 - x_1)^2 + (y_2-y_1)^2}. what I did is
``````

This is what I did with it

``````sqrt{(x_2 - x_1)}
``````

But I was supposed to do this

``````sqrt{(x_2 - x_1)*(x_2 - x_1)}
``````

here it is fixed

``````init = function()
posdistx = sqrt((mouse.x-0)*(mouse.x-0))
posdisty = sqrt((mouse.y-0)*(mouse.y-0))
negdistx = sqrt((0-mouse.x)*(0-mouse.x))
negdisty = sqrt((0-mouse.y)*(0-mouse.y))
end

update = function()
posdistx = sqrt((mouse.x-0)*(mouse.x-0))
posdisty = sqrt((mouse.y-0)*(mouse.y-0))
negdistx = sqrt((0-mouse.x)*(0-mouse.x))
negdisty = sqrt((0-mouse.y)*(0-mouse.y))
end

draw = function()
screen.clear("rgb(0,0,0)")
screen.fillRect(0,0,50,20,"rgb(0,255,0")
if negdistx <= 25 then
if posdistx <= 25 then
if negdisty <= 15 then
if posdisty <= 15 then
screen.drawText("hit"0,0,20,"rgb(255,255,255")
end
end
end
end
end
``````

AABB is the best algorithm for detecting collisions with rectangles if they are parallel to the coordinate axes.

You don't use square root in this algorithm.

You don't ? I was just basing it off the distance equation. Also I am trying to make it so it is all my own code so I don't have to use any libraries

I also just updated the code so it is a whole lot better so I'm just gonna stick to this

``````collision = function(x,y,w,h)
hit = false
posdistx = sqrt((mouse.x-x)*(mouse.x-y))
posdisty = sqrt((mouse.y-y)*(mouse.y-y))
negdistx = sqrt((x-mouse.x)*(x-mouse.x))
negdisty = sqrt((y-mouse.y)*(y-mouse.y))
if negdistx <= w/2 then
if posdistx <= w/2 then
if negdisty <= h/2 then
if posdisty <= h/2 then
hit = true
else
end
end
end
end
end
``````
``````collision = function(x,y,w,h, mouse_x, mouse_y)
hit = false
posdistx = sqrt((mouse_x-x)*(mouse_x-y))
posdisty = sqrt((mouse_y-y)*(mouse_y-y))
negdistx = sqrt((x-mouse_x)*(x-mouse_x))
negdisty = sqrt((y-mouse_y)*(y-mouse_y))
if negdistx <= w/2 then
if posdistx <= w/2 then
if negdisty <= h/2 then
if posdisty <= h/2 then
hit = true
else
end
end
end
end
end

init = function()
screen.fillRect(-80,-10,150,40,"rgb(0,255,0")
for x = - screen.width / 2 to screen.width / 2 by 2
for y = - screen.height /2 to screen.height / 2 by 2
if collision( -80, -10, 150, 40, x, y ) then
screen.drawRound( x, y, 1, 1,"rgb(255,0,0)" )
else
screen.drawRound( x, y, 1, 1,"rgb(255,255,255)" )
end

end
end
screen.drawRect(-80,-10,150,40,"rgb(0,255,0")
end

update = function()

end

draw = function()
if collision( -80, - 10, 150, 40, mouse.x, mouse.y ) then
print( "Hit")
else
print( "no hit ")
end
end
``````

Your code is incorrect. The red area is the one for which your function returns true (it has to be exactly the same as the green rectangle frame). The area returned depends on the location and dimensions of the area you are testing.

AABB is a few lines of code in it

• one is the name of the function
• one starts with return
• one is the end of the function with the word "end"

https://microstudio.dev/i/HomineLudens/aabb/

But all i'm doing is detecting if a mouse is hitting the rectangle, NOT another rectangle.

Here it is detecting two rectangles

``````init = function()
player_x = 0
player_y = -50
block_x = 0
block_y = 0
block_w = 25
player_w = 20
block_h = 20
player_h = 15
hit = false
end

update = function()
collision(player_x,player_y,player_w,player_h,block_x,block_y,block_w,block_h)//end
if keyboard.UP then
player_y = player_y + 1
end
if keyboard.DOWN then
player_y = player_y - 1
end
if keyboard.LEFT then
player_x = player_x - 1
end
if keyboard.RIGHT then
player_x = player_x + 1
end
end

draw = function()
screen.clear()
if hit == false then
screen.fillRect(block_x,block+y,block_w,block_h,"rgb(255,255,0)")
else
screen.drawRect(block_x,block+y,block_w,block_h,"rgb(255,255,0)")
end
screen.fillRect(player_x,player_y,player_w,player_h,"rgb(255,0,0)")
end

collision = function(x1,y1,w1,h2,x2,y2,w2,h2)
hit = false
posdistx = sqrt((x1-x2)*(x1-x2))
posdisty = sqrt((y1-y2)*(y1-y2))
negdistx = sqrt((x2-x1)*(x2-x1))
negdisty = sqrt((y2-y1)*(y2-y1))
if negdistx <= w2 then
if posdistx <= w2 then
if negdisty <= h2 then
if posdisty <= h2 then
hit = true
else
end
end
end
end
end
``````

set

``````  block_x = 50
block_y = 50
``````

and run the code you recently provided. The field returned as true does not match the field seen on the screen.

Check out this code >> https://microstudio.dev/i/microstudio/collisions/ << The official implementation of collision checking - created by MicroStudio developers.

``````init = function()
player_x = 0
player_y = -50
block_x = 0
block_y = 0
block_w = 25
player_w = 20
block_h = 20
player_h = 15
hit = false
end

update = function()
collision(player_x,player_y,player_w,player_h,block_x,block_y,block_w,block_h)//end
if keyboard.UP then
player_y = player_y + 1
end
if keyboard.DOWN then
player_y = player_y - 1
end
if keyboard.LEFT then
player_x = player_x - 1
end
if keyboard.RIGHT then
player_x = player_x + 1
end
end

draw = function()
screen.clear()
if hit == false then
screen.fillRect(block_x,block+y,block_w,block_h,"rgb(255,255,0)")
else
screen.drawRect(block_x,block+y,block_w,block_h,"rgb(255,255,0)")
end
screen.fillRect(player_x,player_y,player_w,player_h,"rgb(255,0,0)")
end

collision = function(x1,y1,w1,h1,x2,y2,w2,h2)
hit = false
posdistx = sqrt((x1-x2)*(x1-x2))
posdisty = sqrt((y1-y2)*(y1-y2))
negdistx = sqrt((x2-x1)*(x2-x1))
negdisty = sqrt((y2-y1)*(y2-y1))
if negdistx <= w2/2 + w1/2 then
if posdistx <= w2/2 + w1/2 then
if negdisty <= h2/2 + h1/2 then
if posdisty <= h2/2 + h1/2 then
hit = true
else
end
end
end
end
end
``````

## Post a reply

Progress

Status

Validate your e-mail address to participate in the community