Rotate and Move to Point

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Rotate and Move to Point

Postby Firefly » Sun Jul 05, 2009 11:50 pm

Heya,

How would I go about making a body rotate to face a point and then move towards it? Basically like in an RTS. At the moment I have the body moving toward the point but I think it could be done better I'm just not sure how and I dont know how to calculate the rotation. I'm also not sure how I should create the body, at the moment its just a sphere so it can roll along the terrain, but this won't work too well when I get the rotation to work. So I guess my main questions are:

1. How to move and rotate body towards a point?
2. What type of collision body should I use to make the units align with the terrain surface as they move?

Heres my code so far:

Code: Select all
void CRTSUnit :: OnApplyForceAndTorque ( float timestep )
{
   dFloat mass;
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;

   NewtonBodyGetMassMatrix (GetBody(), &mass, &Ixx, &Iyy, &Izz);

   D3DXVECTOR3 vPosition = m_vPosition;
   D3DXVECTOR3 vEndPosition = m_vEndPosition;
   D3DXVECTOR3 moveForce(0,0,0);
   vPosition.y = 0;
   vEndPosition.y = 0;

   if ( vPosition != vEndPosition )
   {
      moveForce = vEndPosition - vPosition;
      float fDistance = D3DXVec3Length(&moveForce);

      if ( fDistance < m_fMoveSpeed )
      {
         moveForce.x = 0;
         moveForce.y = 0;
         moveForce.z = 0;
         NewtonBodySetVelocity(m_pBody, &moveForce.x);
      }
      else
      {
         D3DXVec3Normalize(&moveForce, &moveForce);
         moveForce.y = 0;
         moveForce *= mass * m_fMoveSpeed;
      }
   }

   //apply the gravity and other forces
   D3DXVECTOR3 gravity(0.0f, mass * -9.8f, 0.0f);
   gravity += moveForce;
   NewtonBodyAddForce (GetBody(), &gravity.x);
}
Firefly
 
Posts: 32
Joined: Wed Jan 17, 2007 4:58 am

Re: Rotate and Move to Point

Postby Firefly » Wed Jul 08, 2009 4:40 am

*BUMP*

Any ideas Julio?
Firefly
 
Posts: 32
Joined: Wed Jan 17, 2007 4:58 am

Re: Rotate and Move to Point

Postby Julio Jerez » Wed Jul 08, 2009 9:04 am

If you give some time I will reply to that with the p[art tow of the tutorials,
I already integrate the controller into the SDK, but I a holding until I get at least the beginners tutorials completed, before I publish beta 2.04

I believe you question will be answered with the Modified Player controller of 2.0 which is a physics based but act as a pure kinematic objects.
Basically you move it by setting the heading angle and the speed, and the joint do the work.

The reason I am working these tutorial is that they are very focus in one issue, one project on issue, and there will have a word doc to go with and also will be in eth wiki so that people can make corrections.
The simple “One project many demos” solution confuses a lot of people, especially people how do not have much experience in large CPP projects.

I misunderstood you question please ask again.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Rotate and Move to Point

Postby JernejL » Wed Jul 08, 2009 9:15 am

Since RTS units are usually mechanical or biological units they first need a method to.. "turn".

After you have turning capability for units, you simply compare the unit current heading and angle between current location and unit destination and tell it to repeatedly turn by maximum allowed turning force into direction where angle change would be smallest, until they face the target direction.

So your question is not really a physics question but related to general game mechanics, unless you are asking how to make a newton body turn.

You can make a body turn by adding angular omega or by adding specific force & torque that would change the body's direction.

Also, you are using NewtonBodySetVelocity in your code directly, you should use NewtonBodyAddForce , setting velocity directly may cause problems with collision detection.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Rotate and Move to Point

Postby Firefly » Mon Jul 13, 2009 4:56 am

It's more the mathematics behind it that I don't know. If I were to use NewtonBodyAddForce, how I would I given a point move a body towards it and make it stop at that point? And how would I calculate the correct angular omega to point the body to at that point?

Cheers.
Firefly
 
Posts: 32
Joined: Wed Jan 17, 2007 4:58 am

Re: Rotate and Move to Point

Postby kallaspriit » Mon Jul 13, 2009 7:20 am

How about the CustomPickBody joint that comes with NGD2 custom joints? Seems to do almoust that by looking at the header, have not used it though :P

CustomPickBody.h
Code: Select all
//********************************************************************
// Newton Game dynamics
// copyright 2000-2004
// By Julio Jerez
// VC: 6.0
// simple demo list vector class with iterators
//********************************************************************


// CustomPickBody.h: interface for the CustomPickBody class.
//
//////////////////////////////////////////////////////////////////////

#ifndef __CustomPickBody_H__
#define __CustomPickBody_H__


#include "NewtonCustomJoint.h"

class JOINTLIBRARY_API CustomPickBody: public NewtonCustomJoint
{
   public:
   CustomPickBody (const NewtonBody* body, const dVector& handleInGlobalSpace);
   virtual ~CustomPickBody();

   void SetPickMode (int mode);
   void SetMaxLinearFriction(dFloat accel);
   void SetMaxAngularFriction(dFloat alpha);
   
   void SetTargetRotation (const dQuaternion& rotation);
   void SetTargetPosit (const dVector& posit);
   void SetTargetMatrix (const dMatrix& matrix);

   dMatrix GetTargetMatrix () const;

   protected:
   virtual void SubmitConstrainst (dFloat timestep, int threadIndex);
   virtual void GetInfo (NewtonJointRecord* info) const;

   int m_pickMode;
   int m_autoSlepState;
   dFloat m_maxLinearFriction;
   dFloat m_maxAngularFriction;
   dVector m_localHandle;
   dVector m_targetPosit;
   dQuaternion m_targetRot;
};

#endif // !defined(AFX_CustomPickBody_H__EAE1E36C_6FDF_4D86_B4EE_855E3D1046F4__INCLUDED_)
kallaspriit
 
Posts: 216
Joined: Sun Aug 14, 2005 6:31 pm

Re: Rotate and Move to Point

Postby Julio Jerez » Mon Jul 13, 2009 9:16 am

If yo ucheck the nw totorial in teh wiki, teh pick joint is the joint use for raypicking.
I has tow mode of operation left click pick a noy at a point, an dright click pick a body remoing angula velicities.
soon I will righ the doc explaining how it is used, but the code is very simple.
it is the toturial conve cast an dray cast.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 14 guests

cron