Getting two moving characters to collide

Started by barnacleboy, Wed 26/01/2022 22:37:02

Previous topic - Next topic

barnacleboy

I'm trying to get two characters to collide, and it works, but it's not even close to accurate. I mean the blocking areas (which I've massively expanded with BlockingHeight/Width for both characters) will happily pass through each other (as seen with ctrl-a) without colliding most of the time, unless they are quite dead on.

Searching here I found something explaining that moving characters' collision area isn't really the entire blocking height/width area... or maybe I misunderstood that. Sounds weird.

Both characters ARE moving though - how can I best get them to collide if their sprites touch (even just vaguely accurately...)?


Pax Animo

#1
Misunderstood

Khris

Are we talking about rectangle vs. rectangle or do you need pixel-perfect collision detection?

As you found out, the built-in stuff is for a 2.5D game. Is yours a top-down one?

barnacleboy

#3
That post from fernewelten is where I got the part about "characters are represented by just the mid point of their baseline. Every character can walk anywhere so long as the mid point of their baseline stays within the walkable area."

As I mentioned I think that is my problem. I have two characters that I want to collide. In this image they are NOT colliding, though they completely overlap:
https://imgur.com/a/OUplzyO


Here they are just about to collide (the left box is moving directly to the right...can't seem to ctrl-a during the collision). https://imgur.com/a/6ioV6DH

I'm not sure I understand fernewelten's explanation fully (because... when they collide, their "mid point of the baseline" are not overlapping?).

Is there any other way to resolve this than hack one of my characters into an object? It does not have to be pixel perfect, I'm happy as long as the characters dont completely happily pass through each other.

barnacleboy

Well, I've changed one of the characters to an object instead, and now it works. It's not a graceful solution (I basically swap it for the character after the collision), but I can live with it.

Khris

My guess is that each character's position is checked against the other character's solidity rectangle, so the two boxes can definitely overlap without the characters colliding.
The way it works I figure is that when AGS calculates the path of a character, it takes the walkable areas and removes all other characters' blocking areas, so the character's own blocking area is basically ignored. Still, that means the first image you posted shouldn't happen in theory, but I don't know how often AGS amends the path during the walk.

I assume both characters are going to be walking around?

barnacleboy

Quote from: Khris on Thu 27/01/2022 08:39:56
I assume both characters are going to be walking around?

Basically. It's a car driving in a straight line driving towards the character when he crosses the road. As you said, it's a bit weird because the overlap in my first image really doesn't seem logical unless both characters are only judged by a single point, which is what I think fernewelten is saying.

eri0o

#7
Not sure if it's what is needed but there's an obscure AGS Script command I like to use to similar cases, AreThingsOverlapping:

https://adventuregamestudio.github.io/ags-manual/Globalfunctions_Room.html#arethingsoverlapping

Check the See also for two additional also slightly less obscure script commands.

Edit: ah, it's not what is needed, but more like, if someone was completely scripting collision from scratch, then those could be helpful, but not the case here.

fernewelten

#8
Quote from: barnacleboy on Thu 27/01/2022 07:58:58
I'm not sure I understand fernewelten's explanation fully (because... when they collide, their "mid point of the baseline" are not overlapping?).

There might be yet another can of worms here. What I tried to find out when I wrote that article is how characters interact with "solid" objects. Basically, the "solid" objects eat a certain area out of the walkable area, stopping the midpoint of the character's bottom edge from walking there. Colliding might be handled in yet another way that is different from rectangles being taken out of the walkable area.

Also, the question might be how long it takes for characters to stop when a collision is detected.

One of the problems with regions is that there is an event "Player Walks Onto Region". You'd expect the player to instantly stop as soon as this happens, or at least, that the event fires as soon as the player's toes touch the region so that you can call player.StopMoving(), but no: Whenever the character is in a blocking walk, they will blithely continue walking to their destination no matter what, possibly passing right through the region and off on the other side. Basically, if the player is in a blocking walk, you can't even stop them in repeatedly_execute or repeatedly_execute_always. They'll simply ignore the StopMoving() command and continue blithely walking. The event will only fire when the character has stopped their blocking walk on their own and happens to still be on the region.

There might be a similar problem with collisions. Are those checked constantly or only, e.g., when the colliders have stopped on their own? And do colliders stop? You might need to resort to logic such as
Code: ags

player.Walk(x, y, eNoBlock); // note, eNoBlock
while(player.Moving)
{
    if(player.IsCollidingWithObject(oDeadlyMeanCar))
    {
        player.StopMoving();
        oDeadlyMeanCar.StopMoving();
        …
    }
    Wait(1); // important
}

SMF spam blocked by CleanTalk