[SOLVED] Colliding with the edges of a circle-shaped map
Posted: Mon Oct 17, 2022 4:52 pm
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:
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...
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