Rotating and moving towards entity.

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
needshelprendering
Posts: 25
Joined: Mon Sep 17, 2012 1:17 am

Rotating and moving towards entity.

Post by needshelprendering »

Hi Norbo,

I went ahead and searched the forum and found a similar topic but it doesn't seem to have fixed my problem. I am trying to rotate an NPC CharacterController towards an entity and then move towards it.

I have the following code inside the NPC CharacterController's update function:

Code: Select all

Vector3 currentPosition = character.Body.Position;
Vector3 positionToFace = player.Body.Position;
Vector3 difference = positionToFace - currentPosition;
character.ViewDirection = difference;
character.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(new Vector2(difference.Y, difference.Z));
The character seems to face the object because I added this to the draw function (I am using Bepu's test demos to try this)

Code: Select all

Game.LineDrawer.LightingEnabled = false;
Game.LineDrawer.VertexColorEnabled = true;
Game.LineDrawer.World = Microsoft.Xna.Framework.Matrix.Identity;
Game.LineDrawer.View = MathConverter.Convert(Game.Camera.ViewMatrix);
Game.LineDrawer.Projection = MathConverter.Convert(Game.Camera.ProjectionMatrix);
Game.GraphicsDevice.BlendState = BlendState.Opaque;
Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
foreach (var pass in Game.LineDrawer.CurrentTechnique.Passes)
{
	pass.Apply();
	Game.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineList,
	new[]
	{
		new VertexPositionColor(character.Body.Position, Microsoft.Xna.Framework.Color.Blue),
		new VertexPositionColor(character.ViewDirection + character.Body.Position, Microsoft.Xna.Framework.Color.Blue)
	},
	0, 1);
}
The NPC seem to move towards the object for a few seconds but then they run away and hit the edges of the test scene. Any ideas?

Edit: I tried swapping around X,Y, and Z for

Code: Select all

 Vector2.Normalize(new Vector2(difference.Y, difference.Z))
but nothing seems to change how they act.

Thanks.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Rotating and moving towards entity.

Post by Norbo »

The MovementDirection is interpreted in terms of the ViewDirection:
WorldMovementDirection = MovementDirection.X * StrafeDirection + MovementDirection.Y * HorizontalMovementDirection
where StrafeDirection and HorizontalMovementDirection are computed from ViewDirection and the character's down direction. For example, if the ViewDirection is (-1, 0, 0), then a MovementDirection of (0,1) will produce a world space movement of (-1, 0, 0).

(The reason for this is that characters can have arbitrary orientations, and it can be annoying to compute the proper movement direction along the character's horizontal plane when the down direction isn't aligned with one of the three main axes. Usually, it's easier to just grab a ViewDirection from a camera or similar source, and then set the MovementDirection knowing that (1,0) is always 'go to the right' and (0,1) is always 'go forward'.)
needshelprendering
Posts: 25
Joined: Mon Sep 17, 2012 1:17 am

Re: Rotating and moving towards entity.

Post by needshelprendering »

Hi Norbo. I don't think I am at the level where I can tell where to go from there. I can set ViewDirection and then just make MovementDirection a Vector2 with X:1,Y:1 but this makes them take semicircular paths toward my entity.

Any help would be appreciated, thanks.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Rotating and moving towards entity.

Post by Norbo »

A MovementDirection of (1, 1) is telling it to strafe as much as it is telling it to go forward, hence the circling. If you want it to just move forward (in the direction of the view direction), then just use (0, 1).
needshelprendering
Posts: 25
Joined: Mon Sep 17, 2012 1:17 am

Re: Rotating and moving towards entity.

Post by needshelprendering »

Hey Norbo,

It seems to be working.

Thank you.
Post Reply