moving an object via waypoints

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

moving an object via waypoints

Post by elija »

Hello,

Using the BEPU simulation, I would like to know what would be the suggested approach to have an object moved via N waypoints using a transpolated path?
(Such object can be shot at and should bounce off it's flight path(might hit others as well) and then might resume it's flight again.)

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

Re: moving an object via waypoints

Post by Norbo »

The PathFollowingDemo of the BEPUphysicsDemos in the main source download shows one way to get an entity to follow a path.

In the demo, a kinematic entity is used. Kinematic entities have effectively infinite mass and so do not respond to collisions. To allow violation of the path, the entity can be made dynamic and the constraint controlling the entity's motion can be weakened. In the PathFollowingDemo the mover and rotator have a LinearMotor and AngularMotor property, respectively. Those motors are used to control the motion of dynamic entities. For example, if the movingEntity is constructed with a mass of 10 and the EntityMover's motor is weakened by setting lower constants:

Code: Select all

            mover.LinearMotor.Settings.Servo.SpringSettings.DampingConstant = 400;
            mover.LinearMotor.Settings.Servo.SpringSettings.StiffnessConstant = 400;
then the entity will try to reach its goal with some allowed violation.

Note that the paths system and the EntityMover/EntityRotator are just convenience systems. You can use the constraints without using the entity mover/entity rotator, and you can use whatever path system you want to generate the goal locations.
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

Thanks Norbo, for the prompt and detail answer,
I'll check it out.
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

Hello,

I've been playing with the PathFollowingDemo..
I've been able to figure out position calculation along the curve.
I am though unable to figure out how to make the rotator to look at the curve.

Coming from Unity the bellow is what I used to successfully have the ship lookat the flight direction(curve)

Code: Select all

		targetRotation = Quaternion.LookRotation(new_transform.position - last_position, Vector3.up);
			v3 = new_transform.localEulerAngles;
			v3.x = 0f;
			new_transform.localEulerAngles = v3;					
			new_transform.rotation = Quaternion.Slerp(new_transform.rotation, targetRotation, Time.deltaTime * 12f);
			frame_skipper_id = 1;
If you can point me to any information how to achive this with xna/Bepu it'll help alot!

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

Re: moving an object via waypoints

Post by Norbo »

I'm not clear on what the goal is exactly. If you want to create a look-at matrix, the XNA Matrix.CreateLookAt method will do the trick. Note that it creates a view matrix. If you want a world matrix, you'll need to invert the view matrix it gives you. Matrix.CreateWorld is another similar approach.
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

The goal is to have a ship(NPC) flying a curve and always looking at its flight direction.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: moving an object via waypoints

Post by Norbo »

If you first construct the position curve, you can then walk through the curve and evaluate two points- one at the 'current' time, and one a little beyond. Give those points to the CreateLookAt method or process them into input usable with the CreateWorld method. Pick a good Up vector (usually Vector3.Up is fine). You don't actually have to create an explicit orientation curve since you can compute orientation from the position curve at runtime.
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

Hello Norbo,

With the code bellow I am able to see some rotation but it's all wrong.
I also suspect that the lookat matrix contains a translate component as i see the model runs on a much bigger curve than it should.

Update routine

Code: Select all

  protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==ButtonState.Pressed) this.Exit();
            pathTime += Space.TimeStepSettings.TimeStepDuration;
            mover.TargetPosition = positionPath.Evaluate(pathTime);
            Vector3 lookat_position = positionPath.Evaluate(pathTime+15);
            rotation_matrix = Matrix.Invert(Matrix.CreateLookAt(mover.TargetPosition, lookat_position, Vector3.UnitZ));
            rotator.TargetOrientation = Quaternion.CreateFromRotationMatrix(rotation_matrix);
            base.Update(gameTime);
        }
Draw routine

Code: Select all

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);         
            Matrix[] transforms = new Matrix[myModel.Bones.Count];
            myModel.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in myModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * rotation_matrix * Matrix.CreateTranslation(mover.TargetPosition) * Matrix.CreateScale(0.025f);
                    effect.View = viewMatrix;
                    effect.Projection = projMatrix;
                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }

maybe you able to point out my noob errors here..

Thanks
Elija
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

I've took another approch which seems to solve the problem:

Code: Select all

                Vector3 D = (last_position - mover.TargetPosition);
                Vector3 Right = Vector3.Cross(Vector3.UnitZ, D);
                Vector3.Normalize(ref Right, out Right);
                Vector3 Backwards = Vector3.Cross(Right, Vector3.UnitZ);
                Vector3.Normalize(ref Backwards, out Backwards);
                Vector3 Up = Vector3.Cross(Backwards, Right);
                rot_mat = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: moving an object via waypoints

Post by Norbo »

Off the top of my head, here's a few possible reasons why the first look at based attempt didn't work:
-The lookat_position is created from a position 15 units in the future. If the keyframe times are set wide, this might be perfectly fine, but otherwise it could point to something too far off.
-The CreateLookAt does indeed include a translation component. To simplify drawing, it would be a good idea to just draw with the current WorldTransform of the entity rather than the goal state.
-The given up vector might not be correct.

But if you've got something working, great :) If you're curious, the second code you posted is basically the rotational component of the CreateLookAt/CreateWorld methods.
elija
Posts: 7
Joined: Sat Dec 17, 2011 9:49 pm

Re: moving an object via waypoints

Post by elija »

I've also tried different future offsets but it seemed not to improve rotational tracking along the curve.
The chosen Up vector is a result for having the game plane being active only for the X,Y axis. So in my case Z should be the only rotation axis for such plane.
Post Reply