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

SMF spam blocked by CleanTalk