To use GKAgents or not
Asked Answered
R

2

8

I am developing (or atleast trying to develop) a decently big real time tactics game (something similar to RTS) using SpriteKit.

I am using GamePlay kit for pathfinding.

Initially I used SKActions to move the sprites within the Path but fast enough I realized that it was a big mistake.

Then I tried to implement it with GKAgents (this is my current state) I feel that GKAgents are very raw and premature also they are following some strange Newton Law #1 that makes them to move forever (I can't think of any scenario where it would be useful - maybe for presentations at WWDC)

as well as I see that they have some Angular speed to perform rotations which I don't need at all and can't really find how to disable it...

As well as GKBehaviors given a GKGoals seems to make some weird thing...

Setting behavior to avoid obstacles makes my units to joggle around them... Setting behavior with follow path goal completely ignores everything unless the maxPredictionTime is low enough... I am not even willing to tell what happens when I combine both them.

I feel broken...

I feel like I have 2 options now:

1) to struggle more with those agents and trying to make them behave as I wish

2) To roll all the movement on my own with help by GKObstacleGraph and a path Finding (which is buggy as well I have to say at some points the path to the point will generate the most awful path like "go touch that obstacle then reverse touch that one then go to the actual point (which from the beginning could be achieved by a straight line)").

Question is:

What would be the best out of those options ?

Reuter answered 28/9, 2016 at 18:9 Comment(1)
You might be the only person using this, and the first to actually test it in use, too. Bugs in new features of SpriteKit seem to be the norm.Truncheon
T
15

One of the best ways (in SpriteKit/GameplayKit) to get the kind of behavior you're after is to recognize that path planning and path following need not be the same operation. GameplayKit provides tools for both — GKObstacleGraph is good for planning and GKAgent is good for following a planned path — and they work best when you combine the strengths of each.

(It can be a bit misleading that GKAgent provides obstacle avoidance; don't think of this in the same way as finding a route around obstacles, more like reacting to sudden obstacles in your way.)

To put it another way, GKObstacleGraph and GKAgent are like the difference between navigating with a map and safely driving a car. The former is where you decide to take CA-85 and US-101 instead of I-280. (And maybe reevaluate your decision once in awhile — say, to pick a different set of roads around a traffic jam.) The latter is where you, continuously moment-to-moment, change lanes, avoid potholes, pass slower vehicles, slow down for heavy traffic, etc.


In Apple's DemoBots sample code, they break this out into two steps:

  1. Use GKObstacleGraph to do high level path planning. That is, when the bad guys are "here" and the hero is "way over there", and there are some walls in between, select a series of waypoints that roughly approximates a route from here to there.

  2. Use GKAgent behaviors to make the character roughly follow that path while also reacting to other factors (like making the bad guys not step on each other and giving them vaguely realistic movement curves instead of simply following the lines between waypoints).

You can find most of the relevant stuff behind this in TaskBotBehavior.swift in that sample code — start from addGoalsToFollowPath and look at both the places that gets called and the calls it makes.


As for the "moving forever" and "angular speed" issues...

The agent simulation is a weird mix of a motivation analogy (i.e. the agent does what's needed to move it toward where it "wants" within constraints) and a physics system (i.e. those movements are modeled like forces/impulses). If you take away an agent's goals, it doesn't know that it needs to stop — instead, you need to give it a goal of stopping. (That is, a movement speed goal of zero.) There might be a better model than what Apple's chosen here — file bugs if you have suggestions for design improvements.

Angular speed is trickier. The notion of agents' intrinsic physical constraints being sort of analogous to, say, vehicles on land or boats at sea is pretty well baked into the system. It can't really handle things like space fighters that have to reorient to vector their thrust, or walking creatures that can just as happily walk sideways or backwards as forward — at least, not on its own. You can get some mileage toward changing the "feel" of agent movement with the maxAcceleration property, but you're limited by the fact that said property covers both linear and angular acceleration.

Remember, though, that the interface between what the agent system "wants" and what "actually happens" in your game world is under your control. The easiest way to implement GKAgentDelegate is to just sync the velocity and position properties of the agent and the sprite that it represents. However, you don't have to do it that way — you could calculate a different force/impulse and apply it to your sprite.

Triode answered 28/9, 2016 at 19:28 Comment(11)
Sir, I am really appreciate your detailed answer, however I am already done before writing the Question -- What I meant to say is All this framework (let me call it Frameworks please) is so fragile and every single behavior and it's Goal need so much fine tuning (after couple hours of wandering of "wtf" is going on there). That I feel I can roll my own where I can do everything as I need with full control of each element...Reuter
Please don't misunderstand me I am really came to this with open mind, and I tried to accomplish it... And I made some basic FollowPath and Intercept after like 5 hours of playing with prediction times and weights...Reuter
The real question is: is it worth to roll my own, or is it will be better to try and make GameplayKit to behave as I wish ?Reuter
Edited answer with some more tips. However "I am already done before writing the Question" suggests you aren't actually looking for help... in which case you don't have an SO question.Triode
Whether it's "better" to roll your own or find ways to put together existing tools for the behaviors you seek is subjective, so nobody can give you a complete answer. Depends a lot on your use cases, your skills, your performance constraints, what kind of debugging you prefer, etc. We can provide tips about each approach and let you decide for yourself.Triode
Yes this is a great case - it feels really good for having something like cars or boats - but I find it hard to customize for my needs. I put the float_max value for the maxAcceleration and I put 0.01 for the mass -- still without having a Goal to have a Speed(as i want) it takes decades to accelerate -- and still decades to make a turn (I want the turn to be instantly) What i found is just to ignore the rotation, and calculate angle my self given 2 points (before and after position changed) but it feels so wrong performance wiseReuter
I ma not sure about file a bug, maybe I don't see some aspects of the much larger picture of what I see... But honestly it feels just broken to see agent flickering around the goal point :-)Reuter
I think Rickster might be the only person on earth that understands and can articulate GameplayKit. Apple might understand it, but they sure as shit can't articulate it.Truncheon
@Truncheon I decided to roll my own :-) Once again I learned on my back - want something to be done ? -> Do it your self.Reuter
Yeah, SpriteKit and SceneKit are wonderfully tempting/tantalising demonstrations-of-intent and works-in-progress. DOH!Truncheon
@Truncheon These frameworks are new and probably will change hopefully to the better side. I decided to roll my own but in case that I will fail I will be able to revert to GameplayKit it wont run away - I also using GKGraphs for path finding... GKAgent gave me very bad experience, but maybe in a year or 3 they will make it more flexible.Reuter
P
2

I can't comment yet so I post as an answer. I faced the same problem recently: agent wiggling around the target or the agent that keeps moving even if you remove the behavior. Then I realized that the behavior is just the algorithm controling the movement, but you can still access and set the agent's speed, position and angle by hand.

In my case, I have a critter entity that chases for food in the scene. When it makes contact with the food agent, the food entity is removed. I tried many things to make the critter stop after eating the food (it would keep going in a straight line). And all I had to do was to set its speed to 0. That is because the behavior will influence not the position directly, but the speed/angle combination instead (from what I understand). When there is no goal for the entity, it doesn't "want" to change it's state, so whatever speed and direction it reached, it will keep. It will simply not update/change it. So unless you create a goal to make it want to stop, it will wiggle/keep going. The easy way is to set the behavior to nil and set the speed to 0 yourself.

If the behavior/goal system doesn't do it for the type of animation you are looking for, you can still use the Agent system and customize the movement with the AgenDelegate protocol and the update method and make it interact with other agents later on. You can even synchronize the agent with a node that is moved with the physics engine or with actions (or any other way).

I think the agent system is nice to keep around since you can use it later, even if it's only for special effects. But just as mixing actions and physics can give some weird results, mixing goal/behaviors and any other "automated" tool will probably result in an erratic behavior.

You can also use the agent system for other stuff than moving an actual sprite around. For example, you could use an agent to act as a "target seeker" to simulate reaction time for your enemies. The agent moves around the scene and finds other agents, when it makes contact with a suitable target, the enemy entity would attack it (random idea).

It's not a "one size fits all" solution, but it's a very nice tool to have.

Pseudohermaphrodite answered 2/1, 2017 at 6:8 Comment(9)
Thanks for you answer (comment if you wish), but well... I just dumped them and implemented my own "agents" that are perfectly suited for my game - however later on I faced a lot of crashes and glitches in SceneKit that was to deal within it's implementation - also the physics engine wasn't quit good for my needs... for now I am trying to port all I done in to Unreal 4 - I got tired of betaness/alphaness of SceneKit - I have to say that SceneKit really fun engine - (when it works) and many things are done easy... I hope Apple will improve it so 1 day I will be able to distribute a game on itReuter
For what I'm currently doing, SpriteKit is enough. Didn't get into scenekit, so I don't know about it, but it seem aimed to the 1$ app market at the moment even though it can be capable of a lot more. You just need worarounds for the more involved stuff.It will mature though, I'm pretty sure. I didn't update to Sierra yet, but with the new fetures of GameplayKit, it looks like they integrated the entity/component system into SpritrPseudohermaphrodite
For what I'm currently doing, SpriteKit is enough. Didn't get into scenekit, so I don't know about it, but it seem aimed to the 1$ app market at the moment even though it can be capable of a lot more. You just need worarounds for the more involved stuff.It will mature though, I'm pretty sure. I didn't update to Sierra yet, but with the new fetures of GameplayKit, it looks like they integrated the entity/component system into SpriteKit's node architecture. I think it's the same with SceneKit. It's just sad that you have to update the OS to use these features.Pseudohermaphrodite
SpriteKit is awesome framework I already have developed 3 games on it and one POC - that I discontinued in favour of SceneKit (we would go for 3D in the end), but SceneKit still needs some more iterations from Apple In general SceneKit is fine for very little games - but if you want to do something more complicated you will find it lack of features very fast.Reuter
Yes Entity/Component is Agnostic and will work on both, i don't find it sad to upgrade to a new system - I tend to always update - so I see no problem with this, the real problem is: you can not develop for previous OS versions with the new features.Reuter
I tend to update too, don't worry! But coding is a hobby and I can't jeopardize my work stuff to experiment. When I know everything I use is compatible with Sierra I'll update and I won't care about older OS until I come up with the next Angry Bird or something ;)Pseudohermaphrodite
Well I am targeting Mac desktop device - SceneKit is capable to run some Angry Birds style game - But not relatively large Desktop game -> it is just not there yet.Reuter
Also using GKAgent as "Target Seeker" is probably the most unnecessary expensive way to do it :-)Reuter
Haha! Right indeed. We should benchmark the agent system to see how it deals with a lot of entities.Pseudohermaphrodite

© 2022 - 2024 — McMap. All rights reserved.