Page 1 of 1

Enemy Character Controller

Posted: Sat Aug 09, 2014 7:05 pm
by Firefly09
In my game my player is controlled by a character controller. I would like it to be able to collide with enemies which cannot be pushed.

What would be the best way to handle this? Would it be possible to also represent enemies with a character controller so I can make them walk, while ensuring that the player can neither walk through them or push them?

Re: Enemy Character Controller

Posted: Sat Aug 09, 2014 7:18 pm
by Norbo
The easiest approach would be to increase the mass of the enemies relative to the player and adjust the characters' force-related properties proportionally. This wouldn't make it impossible to push the enemies, but it would be a lot more difficult. (Increasing the force applied by the constraints without increasing mass would also make them hard to push, but they would accelerate faster due to greater force applied to the same mass.)

If the enemies should be truly impossible to push by the player, a somewhat-nonphysical solution will be required. One option would be to disable collision between the enemies' character controller bodies and the player's body, and then make kinematic entities which exactly follow the enemy characters' movement by setting their positions and velocities every frame to match. These kinematic entities would have their collision rules set to only collide with the player, ensuring that enemies would still behave as expected when walking around other dynamic objects.

Re: Enemy Character Controller

Posted: Sat Aug 09, 2014 7:46 pm
by Firefly09
Hi Norbo,

Thanks for the lightening fast reply!

I considered greatly increasing the mass but was concerned this could lead to enemies behaving weirdly due to gravity, for example when falling off of large ledges. Is this concern unfounded or is there anyway to overcome this sort of problem?

I'm not sure I follow what you mean by kinematic entities? Do you mean create something like a cylinder shape?

Again thanks for your help!

Re: Enemy Character Controller

Posted: Sat Aug 09, 2014 9:29 pm
by Norbo
I considered greatly increasing the mass but was concerned this could lead to enemies behaving weirdly due to gravity, for example when falling off of large ledges. Is this concern unfounded or is there anyway to overcome this sort of problem?
Fall speed will be unaffected by the mass, since gravity applies a constant acceleration to all objects.

One potential issue would be mass ratios. Very heavy objects depending on light objects can cause problems for the solver. For example, if a very heavy enemy tried to stand on a light box, the box could get squished out weirdly. Keeping masses within a factor of 10 of each other is a good idea.
I'm not sure I follow what you mean by kinematic entities? Do you mean create something like a cylinder shape?
Kinematic entities are entities which have effectively infinite mass. In other words, they go exactly where their velocity takes them, and no physical force can change their motion.

Creating an entity without specifying a mass, setting an entity's mass to 0, or calling entity.BecomeKinematic all produce a kinematic entity. So doing something like this would work:

Code: Select all

            var kinematicCylinder = new Cylinder(enemy.CharacterController.Body.Position, enemy.CharacterController.Body.Height, enemy.CharacterController.Body.Radius);
            //Make the kinematic object collide only with the player.
            kinematicCylinder.CollisionInformation.CollisionRules.Personal = CollisionRule.NoBroadPhase;
            CollisionRules.AddRule(kinematicCylinder, player.CharacterController.Body, CollisionRule.Normal);
...
            //Setting the position and velocity every frame keeps the proxy in sync with the actual character.
            kinematicCylinder.Position = enemy.CharacterController.Body.Position;
            kinematicCylinder.LinearVelocity = enemy.CharacterController.Body.LinearVelocity;
...
            //If the character can crouch or otherwise change shape, setting these when the shape changes could be needed:
            kinematicCylinder.Height = enemy.CharacterController.Body.Height;
            kinematicCylinder.Radius = enemy.CharacterController.Body.Radius;

Re: Enemy Character Controller

Posted: Sun Aug 10, 2014 6:12 pm
by Firefly09
Norbo wrote:Fall speed will be unaffected by the mass, since gravity applies a constant acceleration to all objects.

:oops: Would you believe I minored in physics at university... Increasing the mass tenfold did the trick, thank you so much for your help!