How to handle collision resolutions?

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.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to make a Wait function? :s

Post by onedaysnotice »

Thanks so much :D I promise to actually study the language when I finished high school :D

So just making this clear, for an IF statement, if the process of its insides results in the condition being contradicted, execution will continue all the way to the end of the IF statement and only then can it exit?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a Wait function? :s

Post by Robin »

onedaysnotice wrote:So just making this clear, for an IF statement, if the process of its insides results in the condition being contradicted, execution will continue all the way to the end of the IF statement and only then can it exit?
Not sure what you mean by this. Could you give an example?
Help us help you: attach a .love.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to make a Wait function? :s

Post by onedaysnotice »

Robin wrote:Not sure what you mean by this. Could you give an example?
Like even for this

Code: Select all

function time:update(dt)
   if self.waiting then
      self.elapsed = self.elapsed + dt
      if self.elapsed >= self.total then
         self.elapsed = 0
         self.waiting = false
         return true -- NOT "return true end", that makes no sense
                     -- also notice you need to return AFTER you reset those properties, not before
      end
   end
end
you can see the condition for the first IF statement is 'if self.waiting == true'. However, self.waiting becomes false in the middle of the second IF statement which is nested inside the first, but execution still continues to 'return true'. The same goes for the second IF condition. Immediately after the condition, self.elapsed is no longer greater than or equal to self.total, and yet 'self.waiting = false' and 'return true' is still executed.

So from what I've gathered here is that if execution is within an IF statement and while inside, if the IF statement's conditions are not met, execution will still continue until the end of the IF statement and then only then can it exit. This is as opposed to if the conditions are not met, execution will immediately exit the IF statement regardless of whether there is any processes after it, as if there was a Break statement there. Is this correct?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a Wait function? :s

Post by Robin »

Maybe it helps to look at it like this:

This:

Code: Select all

if self.elapsed >= self.total then
   self.elapsed = 0
   self.waiting = false
   return true
end
Is really this: (not valid Lua, just to give you an idea how it works under the hood)

Code: Select all

if not (self.elapsed >= self.total) jump to label1
self.elapsed = 0
self.waiting = false
return true
label1
You see, once you pass the first check, the code does no further checks.
Help us help you: attach a .love.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

How to make a Wait function? :s again. lol

Post by onedaysnotice »

Oh i see. That makes things much clearer. Thanks :D

---

Oh, and btw, my wait function is not working entirely as I want it to :( I've done some slight changes to what you gave me, but it still doesn't work right.

Code: Select all

local time = {}
time.__index = time

function newWait(seconds)
   local t = setmetatable({}, time) 
   t.elapsed = 0
   t.total = seconds
   t.waiting = false
   t.finished = false
   return t 
end

function time:update(dt)
   if self.waiting then
      self.elapsed = self.elapsed + dt
      if self.elapsed >= self.total then
         self.elapsed = 0
         self.waiting = false
         self.finished = true
         return true
      end
   end
end

function time:reset()
	self.elapsed = 0
	self.finished = false
end

function time:wait()
	self.elapsed = 0
	self.finished = false
	self.waiting = true
end

function time:waitFinished()
	return self.finished
end
If I make the condition of an if statement for the wait function to be false, like:

Code: Select all

quartersec:update(dt) ==> (i just put this line somewhere around the top of my love.update() function)
...
quartersec:wait()
if not quartersec:waitFinished() then
...
end
it works perfectly fine. However, if i make the condition for it to be true, it doesn't work. Like:

Code: Select all

if quartersec:waitFinished() then
...
end
What needs to be changed so that true conditions would work as well? :s
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a Wait function? :s again lol.

Post by Robin »

  1. In the future, please always upload a .love of what you're doing. This makes things much easier for everyone involved.
  2. First, you call quartersec:wait(). There quartersec.finished is set to false. Then, quartersec:waitFinished() returns quartersec.finished, which you just established is false. Thus, it always returns false and never executes the then branch.

    Solution: remove the call to quartersec:wait() and place it where the timer should start.
Help us help you: attach a .love.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How to make a Wait function? :s again lol.

Post by onedaysnotice »

Robin wrote:
  1. In the future, please always upload a .love of what you're doing. This makes things much easier for everyone involved.
  2. First, you call quartersec:wait(). There quartersec.finished is set to false. Then, quartersec:waitFinished() returns quartersec.finished, which you just established is false. Thus, it always returns false and never executes the then branch.

    Solution: remove the call to quartersec:wait() and place it where the timer should start.
Sorry for the late response. I was at a skiing trip with family and friends. I'll make sure to upload a .love next time :D

Whilst offline coding at the trip during free time, I decided to scrap the wait function idea and stick with the fundamentals that I know. Better to have an unoptimised working product than a half-baked, minutely perceivably optimised, dysfunctional product lol, since the project is due the week after next. Damn me and my severe procrastination Dx.

Thanks for the continued help though, Robin. :)

-----------------------------

Issue #4 - How to handle collision resolution?

STATUS: Unresolved.

I'm just sticking to box collision for now, so the detection part is completely fine. However, the resolution part is what is causing me grief. Right now, I have resolved most my problems with the collision resolution. There is still a multitude of problems however, namely the teleporting when jumping towards the opponent when already touching them. There is also the occasional issue where the player will get stuck on the opponent's top right/left corner and both will move in a uniform direction and speed. Oh there's also the one about jumping on a person thats next to the wall, so it makes the pushing of the top char not occur, but I plan on working on it right now.

I know my code is incredibly hideous, but can you guys please look into what is causing the other glitches? Well I understand why its happening, but I don't really have an idea as to how to go about to solving them. Is there a simpler way of tackling this collision resolution whilst still providing the same functionalities, without the use of libraries because I'm really trying to minimise my use of them, at least for this project. Thanks. :)

wasd to move player 1, arrow keys to move player 2.
Attachments
Archive.love
(5.55 KiB) Downloaded 108 times
Post Reply

Who is online

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