[SOLVED] Colliding with the edges of a circle-shaped map

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

[SOLVED] Colliding with the edges of a circle-shaped map

Post by Bigfoot71 »

Hi there ! ^^

I have a little math problem, I'm trying to make a game that takes place in a map in the shape of a circle and if the player is outside the circle he has to reposition himself at the correct position on the edge (I'm trying to do a circle collision in short), here's an example of what I tried:

Code: Select all

            -- Apply movement --

            local vel = Vec(
                math.sin(self.dir) * self.speed,
                -math.cos(self.dir) * self.speed
            )

            self.pos = self.pos + vel * dt

            -- Collision with map edges --

            local distX = self.pos.x - self.map.x
            local distY = self.pos.y - self.map.y

            local dist = math.sqrt(distX ^ 2 + distY ^ 2)

            if dist >= self.map.r then -- self.map.r : radius of map

                self.pos.x = self.pos.x - distX / dist; 
                self.pos.y = self.pos.y - distY / dist;

            end
If someone has a link to a course or has already done it and could guide me on the correct calculation that would be really cool, otherwise I'll post my solution if I ever get there finally although I've been there for a while a long time already...
Last edited by Bigfoot71 on Mon Oct 17, 2022 6:47 pm, edited 1 time in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Colliding with the edges of a circle-shaped map

Post by darkfrei »

Maybe the static solution
function pushback (collision)
https://github.com/darkfrei/love2d-lua- ... n.lua#L130

Code: Select all

function is_overlap (particles)
	local dx = particles[2].x - particles[1].x 
	local dy = particles[2].y - particles[1].y
	local sdistance = dx*dx+dy*dy
	local b = (particles[1].radius+particles[2].radius)^2
	if not (sdistance <= (b+0.000001)) then return false end
	
	local distance = math.sqrt(sdistance)
	local overlap = -0.5*(distance - particles[1].radius - particles[2].radius)
	local nx = dx/distance -- normalizetion
	local ny = dy/distance
	
	local minid = math.min (particles[1].id, particles[2].id)
	local maxid = math.max (particles[1].id, particles[2].id)
	collision_map[minid] = collision_map[minid] or {}
	if not collision_map[minid][maxid] then
		collision_map[minid][maxid] = {
			particles ={particles[1], particles[2]},
			overlap = overlap, nx=nx, ny=ny
			}
	end
	return particles
end

Code: Select all

function pushback (collision)
	local particles = collision.particles
	local b1, b2 = particles[1], particles[2]
	local overlap = collision.overlap
	local nx, ny = collision.nx, collision.ny 
	b1.x = b1.x - nx*overlap
	b2.x = b2.x + nx*overlap
	b1.y = b1.y - ny*overlap
	b2.y = b2.y + ny*overlap
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

Re: Colliding with the edges of a circle-shaped map

Post by Bigfoot71 »

Thank you very much, it finally works!

Here's how I adapted it to put the position of the player back in the circle if it ever helps someone else understand:

Code: Select all

            -- Collide with map edges --

            local distX = self.pos.x - self.map.x
            local distY = self.pos.y - self.map.y

            local dist = math.sqrt(distX ^ 2 + distY ^ 2)

            if dist >= self.map.r then

                local nx = distX / dist
                local ny = distY / dist

                local overlap = 0.5 * (dist - self.map.r)

                self.pos.x = self.pos.x - nx*overlap
                self.pos.y = self.pos.y - ny*overlap

            end
Last edited by Bigfoot71 on Mon Oct 17, 2022 8:16 pm, edited 3 times in total.
My avatar code for the curious :D V1, V2, V3.
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: Colliding with the edges of a circle-shaped map

Post by pgimeno »

Edit: oops. I didn't notice it was already solved.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 16 guests