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:
-- 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.
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