Is there a way to calculate the closest point from a character to an area?

Started by TheManInBoots, Wed 28/08/2019 18:12:29

Previous topic - Next topic

TheManInBoots

Hi all!

Is there a way to calculate the point of a walkable area that is closest to the character?
It would help if the character for example ends up outside the walkable area due to a random animation or view change, and so that I can correct that and make the character walk back to the walkable area in the most efficient way, especially since the walkable area is shaped in a complex way.

eri0o

Place a dummy character in the same position of the character and do cDummy.PlaceOnWalkableArea() and calculate the distance between the two.

morganw

I think that technically you could note the current co-ordinates of the player, then call PlaceOnWalkableArea, note the player's co-ordinates again, and then place them back in the first position.
(if you wanted to avoid the need for a dummy character)

TheManInBoots

OK cool, the PlaceonWalkableArea function sounds like it's just what I need! Thanks! I'll try it out!

TheManInBoots

Is there btw. a way to find out the distance to a SPECIFIC walkable area? That would make it easier, because then I don't have to remove all the nearby walkable areas around the character just to find out the closest coordinate to the walkable area that the character should actually stand on.

Mandle

You could use the dummy character method and just put one on every area.

Personally I would just manually test for the distance from the character to each of the area's coordinates using Pythagoras' formula instead of using dummy characters.

Crimson Wizard

Quote from: Mandle on Thu 29/08/2019 02:14:46
Personally I would just manually test for the distance from the character to each of the area's coordinates using Pythagoras' formula instead of using dummy characters.

The point is in knowing these coordinates, which AGS does not provide. You'd have to ask AGS for walkable area ID per each pixel around character while increasing radius until you get the first walkable pixel.
This is roughly what AGS is doing in PlaceOnWalkableArea, and it will do it much faster natively than script.

Mandle

Quote from: Crimson Wizard on Thu 29/08/2019 03:03:06
Quote from: Mandle on Thu 29/08/2019 02:14:46
Personally I would just manually test for the distance from the character to each of the area's coordinates using Pythagoras' formula instead of using dummy characters.

The point is in knowing these coordinates, which AGS does not provide. You'd have to ask AGS for walkable area ID per each pixel around character while increasing radius until you get the first walkable pixel.
This is roughly what AGS is doing in PlaceOnWalkableArea, and it will do it much faster natively than script.

Oh, yeah that's the case if he needs the closest walkable pixel in each area.

I was just assuming he needed a rough guide to the center of each walkable area.

Khris

Quote from: TheManInBoots on Wed 28/08/2019 18:12:29It would help if the character for example ends up outside the walkable area due to a random animation or view change

That sounds like you're trying to solve a problem that should never occur in the first place. Animating or changing the view doesn't change the character's position; if you have an animation that's misaligned and you're doing something like  player.x += 5;  beforehand, the solution is to properly center the frames, not mess around with dummy characters and the like.

TheManInBoots

Quote from: Crimson Wizard on Thu 29/08/2019 03:03:06
Quote from: Mandle on Thu 29/08/2019 02:14:46
Personally I would just manually test for the distance from the character to each of the area's coordinates using Pythagoras' formula instead of using dummy characters.

The point is in knowing these coordinates, which AGS does not provide. You'd have to ask AGS for walkable area ID per each pixel around character while increasing radius until you get the first walkable pixel.
This is roughly what AGS is doing in PlaceOnWalkableArea, and it will do it much faster natively than script.

Right now I just remove the other Walkable Areas around to find the closest point to the wanted Walkable area while using the PlaceOnWalkableArea function.
But that sounds really interesting, how would I script that if I really would basically want to use the PlaceOnWalkableArea manually and script it myself as you said?
Would I have to use C language actually or are AGS commands sufficient?

TheManInBoots

Quote from: Khris on Thu 29/08/2019 10:29:39
Quote from: TheManInBoots on Wed 28/08/2019 18:12:29It would help if the character for example ends up outside the walkable area due to a random animation or view change

That sounds like you're trying to solve a problem that should never occur in the first place. Animating or changing the view doesn't change the character's position; if you have an animation that's misaligned and you're doing something like  player.x += 5;  beforehand, the solution is to properly center the frames, not mess around with dummy characters and the like.

if the character changes view picking up, carrying a long object it changes sprite dimensions posiiton etc. with shadow and y coordinate chandge, inaccuracies due to scaling . very useful dummies

TheManInBoots

eri
Quote from: eri0o on Wed 28/08/2019 18:20:35
Place a dummy character in the same position of the character and do cDummy.PlaceOnWalkableArea() and calculate the distance between the two.
Quote from: morganw on Wed 28/08/2019 21:01:53
I think that technically you could note the current co-ordinates of the player, then call PlaceOnWalkableArea, note the player's co-ordinates again, and then place them back in the first position.
(if you wanted to avoid the need for a dummy character)

Thanks eri0o and morganw! I implemented the PlaceOnWalkableArea() function and it works like a charm!!! Thanks a lot!

TheManInBoots

Quote from: Mandle on Thu 29/08/2019 02:14:46
You could use the dummy character method and just put one on every area.

Personally I would just manually test for the distance from the character to each of the area's coordinates using Pythagoras' formula instead of using dummy characters.

I like your idea! How can I access and address all the coordinates of the Walkable area and let the computer run through all of them one by one? What command or function can I use for that?

And for your elaborate dummy method: when my character stands inbetween two Walkable areas, outside any Walkable area, I want the closest coordinate to a specific Walkable area, not necessarily the Walkable Area closest to the character. I don't understand how multiple dummies would help here?

Cassiebsg

What Khris meant, is that you should draw your characters sprites always centered to the same spot. It's feet should be in the same position no matter how more pixels your animation needs. If you need 50 pixels more to the right, you just add 50 empty pixels to the left as well, so that the feet stay in place.  ;)
There are those who believe that life here began out there...

eri0o

I do exactly what Cassiebsg said and it works great. The only time I needed something more complex I used a dummy object from the room and made my character transparent.

TheManInBoots

At one point my character is shoving a big rock in front of him.
Which adds a big shadow below the character and the rock, that I added in the view animation,
which means that the character center on the y-coordinate is dislocated, even if the character remains centered on the left and right.

Of course there are multiple ways to manage that and I might figure out a way to  add the shadow separately as a different character that follows the first character so that I can keep the character position centered to the same spot, but helas, it gets complicated again.
So I just choose to dislocate the character during the view switch, and that's the problem I was dealing with right now.
When you create a game on the go, and don't know what's gonna come in the end, you sometimes get new ideas, and it's sometimes easier to just dislocate the character instead of readjusting all of your previous sprites because you didn't know in the beginning that you gonna do a certain type of animation. Also it's really impractical to have the character's center dislocated on the y-coordinate in the main walking view, because then he always walks above the mouse click and not exactly where you clicked, and you have to take that into account as well. So the whole thing is just way more complex, and just adding the advice to center your character is not really helpful for the issue I had. So if my question was how to center and animate my characters better during view switch, I would have done so. And if Khris wants to talk about that he can do so where it's needed and actually helpful, but it's not what this thread was about.
So I prefer focusing on the stuff here in the forum that actually helps me with my actual and concrete problems with my game.

Keeping the character's  alignment point centered to the base point of the sprite is a great idea. But that was not my concern here, so it's not really the issue...

Cassiebsg

You do know that characters have a z property, right? So all you would need would be to adjust the z coord and not the y.
Also making a shadow character, would have been way easier (since you already had the sprite done), since you could have just told it to "FollowExactly" your main character.  ;)
But sure, get back on topic.
There are those who believe that life here began out there...

eri0o

Ah, you can reimport the sprites from source too. :) It's on the right click menu of the mouse.

Infinite solutions :D

TheManInBoots

Okay, I have to say you guys are right. My bad. And sorry Khris, you were right, I could have arranged my sprites way better to avoid this problem.

TheManInBoots

I'm still fascinated by the topic of the walkable areas though!  :-D

Khris

No need to apologize; I was actually hesitant to post that because I thought I might not have the full picture.
It's just that after doing this for so long, I always see XY problems everywhere :-D :P

Cassiebsg

Also, it wasn't meant like "go fix your sprites", but more like "next time, you know there are other options so you don't get into a coding problem."  ;)
There are those who believe that life here began out there...

Crimson Wizard

Have anyone tried LockViewAligned and LockViewOffset as an alternative to manually add empty regions to sprites?

E.g. LockViewAligned:

QuoteThe main purpose of this command is that it can align the new frame to the previous one. This is particularly useful if you want to go from the character's normal walking view to a specific animation - since characters have the central point as their 'axis', if you have a wider animation then it can be difficult to stop yourself getting a jumping effect when the animation starts.

Cassiebsg

I didn't even knew that existed, much less use it.  8-0
But maybe that's exactly what the author of this thread needs?  :)
There are those who believe that life here began out there...

Khris

It depends on the sprites I guess; if they are not hugely off-center, centering the sprites is preferable. But if the animation required huge transparent areas on one side to keep the feet centered, using an aligned view seems the better solution.

TheManInBoots

Quote from: Crimson Wizard on Fri 30/08/2019 16:21:14
Have anyone tried LockViewAligned and LockViewOffset as an alternative to manually add empty regions to sprites?

E.g. LockViewAligned:

QuoteThe main purpose of this command is that it can align the new frame to the previous one. This is particularly useful if you want to go from the character's normal walking view to a specific animation - since characters have the central point as their 'axis', if you have a wider animation then it can be difficult to stop yourself getting a jumping effect when the animation starts.

Doesn't really help me in my case.
It simplifies placing the character in the right place and can make a cleaner script, but the character still ends up outside the walkable area this way.

TheManInBoots

Quote from: Khris on Fri 30/08/2019 00:51:35
No need to apologize; I was actually hesitant to post that because I thought I might not have the full picture.
It's just that after doing this for so long, I always see XY problems everywhere :-D :P

Just don't be a dick when sharing your ideas, like when you said we were just " messing around".
That's one of the main forum rules to give constructive feedback.
You don't know everything and even with the right sprite orientation I still need the dummies and function.

Khris

I wasn't my intention to label what you are doing as "messing around".
What I meant was that knowing about all possibilities and picking the optimal solution is better than using a messy one, working or not.

Of course I don't know everything; sometimes other people suggest solutions I wasn't even aware of existed.
And sometimes other people suggest solutions I consider messy, and since this isn't the critics lounge but the technical forum, I'll chime in in such a case.

TheManInBoots

Okay, no worries then.

For most view changes it is helpful to change the sprites' orientation in my game and use all the previous tips and ideas, but I still have one animation left for which I haven't found a better solution than actually using the dummy and the "PlaceOnWalkableArea" function.
That's when the character is being catapulted by a big metal spring. He is being catapulted in "3d" space so to speak, diagonally to the back of the room, so he's walking against the spring and then being pushed by the spring forwards and flying away, and becoming smaller while flying away obviously. So when the character detaches from the spring, the solution with the "PlaceOnWalkableArea" function is the best one in my opinion for two reasons:

1) Editing the sprites messes up the scaling: If I aligned all the sprites to have the same y-baseline (the sprites with the metal spring being bent by the character, and the sprites with character detached from the metal spring by himself), it would completely mess up the scaling of my character, because the baseline of the spring is significantly bigger (and closer to the screen) than the character himself, so the character's scaling has to be smaller than the scaling of the metal spring. Fixing that through ManualScaling would be extremely messy because then I cannot use the intuitive scaling of the walkable areas anymore in the follow up and would have to create a complete manual Scaling system, for every single Walkable area individually, and that would be really messy.
And just in general having too big of a z-offset for my character might affect the scaling too much.

2) When you place the metal spring somewhere, even when the baseline of the metal spring is inside the walkable area, the "catapulting" end of the spring might still be outside the walkable area, because the spring is lying diagonally towards the back of the screen in space. So when you are catapulting yourself, you will end up outside the walkable area. Now when that happens and the spring is lying in front of the truck for example with the end outside of the walkable area, I will move the character first into the walkable area before playing the view that shows the character "crashing" against the truck. Maybe you could also move the character while animating the crash view or after it. Because if you don't move the character, then the character is floating in space in front of the truck after the crash animation, and is standing outside the walkable area.

I don't know if there's a better way to do this.

SMF spam blocked by CleanTalk