How do you implement NPC (non-playable character)?

Hi dudes!

How do you implement NPC (non-playable character) with box2d in a 2d platform game?

I have 2 ideas, what do you think?

1st. Put 2 points (start and end) in my level editor and when exceed, just turn around.

2nd. Use a ray and if I detect there is no platform (or there is a wall), NPC turn around.

How do you suggest I move my NPCs? ApplyImpulse? ApplyForce? SetLinearSpeed?

If I use ApplyForce every Update call, they will accelerate to the infinity and beyond.
SetLinearSpeed violates the actual physic simulation.

What do you think?

Thanks for help.
What you may want to look up is the A* (verbally "A Star") algorithm in order to make the NPC find points to navigate to. This of course will also require the implementation of a scene graph, but that which does not kill you will only make you stronger eh? Anyway, you should manually set the vector of the body on the X axis with SetLinearSpeed and fake the gravity using collision callbacks to nullify the "gravity". You should not think of Box2D as your one stop shop of solutions, more like a toolkit to work with. In this case, all you really need are the collision callbacks, bodies, and shapes, and then fake everything else.
Orm wrote:What you may want to look up is the A* (verbally "A Star") algorithm in order to make the NPC find points to navigate to. This of course will also require the implementation of a scene graph, but that which does not kill you will only make you stronger eh? Anyway, you should manually set the vector of the body on the X axis with SetLinearSpeed and fake the gravity using collision callbacks to nullify the "gravity". You should not think of Box2D as your one stop shop of solutions, more like a toolkit to work with. In this case, all you really need are the collision callbacks, bodies, and shapes, and then fake everything else.


It sounds like all he wants is a character to walk back and forth, like in Mario. I think A* is overkill (and if I'm not mistaken, a little difficult to implement without a grid?). Also, why do you need to fake gravity? Shouldn't the character move with the curvature of the terrain as normal?

I think I'd go with what the author suggested, and try casting a short ray to figure out if there's more ground beneath his feet. Maybe the NPC should turn-around when there's either no ground up ahead, or it's steeper than 45 degrees (so he doesn't slip off into oblivion).

You can use SetLinearSpeed (I thought it was named Velocity?) or Apply a Force or impulse; you just need to calculate what force/impulse to add so that it doesn't keep accelerating him. Requires a bit of math, but nothing too hard.
darkzerox wrote:
Orm wrote:What you may want to look up is the A* (verbally "A Star") algorithm in order to make the NPC find points to navigate to. This of course will also require the implementation of a scene graph, but that which does not kill you will only make you stronger eh? Anyway, you should manually set the vector of the body on the X axis with SetLinearSpeed and fake the gravity using collision callbacks to nullify the "gravity". You should not think of Box2D as your one stop shop of solutions, more like a toolkit to work with. In this case, all you really need are the collision callbacks, bodies, and shapes, and then fake everything else.


It sounds like all he wants is a character to walk back and forth, like in Mario. I think A* is overkill (and if I'm not mistaken, a little difficult to implement without a grid?). Also, why do you need to fake gravity? Shouldn't the character move with the curvature of the terrain as normal?

I think I'd go with what the author suggested, and try casting a short ray to figure out if there's more ground beneath his feet. Maybe the NPC should turn-around when there's either no ground up ahead, or it's steeper than 45 degrees (so he doesn't slip off into oblivion).

You can use SetLinearSpeed (I thought it was named Velocity?) or Apply a Force or impulse; you just need to calculate what force/impulse to add so that it doesn't keep accelerating him. Requires a bit of math, but nothing too hard.


Thanks for replies.
Yes, something like Mario, I guess A* is not necessary for my problem.
What I don't know, is how exactly implement it.

I guess using sensors or raycast should not be complicated in order to send events to my FSM and change my NPC direction.

But, darkzerox, can you explain me better how to implement my NPC movement? I mean, how is it done in actual games?

Thanks.
HI

These non-playable characters were used to demonstrate “behaviors” such as My npc's are little ladybird bugs. I also decided to have a fence around my world they implement something for the undo option which goes back only ONCE! NPC's (Non Playable Characters). Brown-G: Father of Red and Blue. Appears climbing on a metal bird (Ferric Falcon) at the beginning of the ..


Regards










job a la maison - porte bebe hamac
ricardo_ruiz_lopez wrote:But, darkzerox, can you explain me better how to implement my NPC movement? I mean, how is it done in actual games?

Thanks.


I haven't a clue how it's done in real games. I'm a noob too :p All I've got is theory and very little hands-on experience. Maybe you can have some sort of "behaviour" class which you can attach to your NPCs, and then create a bunch of behaviour scripts and just attach them to whatever monster you want, so they're re-usable. The code would be fairly simple. Just something like

- static bool walkingLeft = true
- check if ground up ahead
- if true, apply force left
- if false, walkingLeft = false, flip sprite, apply force right...

whatever, something like that. then you just run a "step" of that during every frame for all your (active) NPCs.

Original thread: https://box2d.org/forum/viewtopic.php?t=3957