[SOLVED] Collision Line (or something similar)

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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Collision Line (or something similar)

Post by kikito »

CRxTRDude wrote:@kikito Hello again, i saw the code, it's looking fine, but i saw getObjectsInRoom and i don't know what function it is ... Is that a set of tables or something else?
I called it getObjectsInRoom, but you could also call it getAllOjects. It should return a list of potential objects that could be intersecting with the segment. It could be the list of all objects in the game (that would be slower, but it's a good first value).
When I write def I mean function.
User avatar
CRxTRDude
Prole
Posts: 41
Joined: Sun Dec 15, 2013 3:03 am
Location: Some island in the archipelago

Re: [SOLVED] Collision Line (or something similar)

Post by CRxTRDude »

Hey guys, for my implementation, I'll share it to you guys:

All of these are in the enemy's code, not the player's code!

For the distance check:

Code: Select all

function Thug:curDist(x1,y1,x2,y2)
  local calc_x = math.abs(x1-x2)
  local calc_y = math.abs(y1-y2)
  local calc = math.sqrt((calc_x^2) + (calc_y^2))
  return math.floor(calc)
end
from this site...

as usual [x1,y1] is the player's coords and [x2,y2] is the enemy's.

then the y check:

Code: Select all

function Thug:yHit()
  if ((p.y+150) >= (self.y)) and ((p.y) <=(self.y+32)) then
    return true
  else return false
  end
end
this checks if the player's is hit vertically by your enemy, the code here gives the enemy a range of 150 px for his vertical LOS.

finally, the code for facing:

Code: Select all

function Thug:getPlayerFacing(pl)
  local facing = false
  if (self.x_vel < 0) and (pl.x < self.x) then
    facing = true
  elseif (self.x_vel < 0) and (pl.x > self.x) then
    facing = false
  elseif (self.x_vel > 0) and (pl.x > self.x) then
    facing = true
  elseif (self.x_vel > 0) and (pl.x < self.x) then
    facing = false
  end
  self.towards = facing
end
Found this from a GM tutorial. This will check if the player and enemy are facing each other.

Now for implementing it to the code:

Code: Select all

      dist = self:curDist(p.x, p.y, self.x, self.y) -- updates the player's distance

      self:getPlayerFacing(p) -- my implementation of if the enemy faces the player.
      
      -- check your distances
      if self.towards and self:yHit() then -- using our vertical LOS and a towards check
        if dist <= 150 then                       -- the distance is just close enough...
          self.state = 2                            --  to start a chase
        end
      end
That's my implementation so far. If it's helpful to some, use it. But remember to credit me for some of that. (I placed it in QUEUE and it's under the MIT license so you can use it anytime, but credit it. See this for details.)
~ CRxTRDude || Will be out of touch for a wee longer than expected. Will keep in touch with soon enough. Sorry bout that.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest