Hello, the following code is programmed by Tolk, and I need your help to settle the angle problem.
The code shows a capture a mouse clicking and the player is driven to the direction of mouse clicking position immediately.
if(dbMouseClick()) {
//Player is now moving
moving = true;
// Pick the mouse location on 2D screen
int pickVector = dbPickObject ( dbMouseX (), dbMouseY (), 1, 1 ) ;
//Pick the x and z location on 3d terrain of mouse location
float xHit = dbGetPickVectorX() + dbCameraPositionX();
float zHit = dbGetPickVectorZ() + dbCameraPositionZ();
//Vector from origin to mouse location
dbSetVector2(1, xHit, zHit );
//Vector from origin to object location
dbSetVector2(2, dbObjectPositionX(3), dbObjectPositionZ(3) );
//Vector describing movement from object to mouse location
dbSubtractVector2(1, 1, 2);
//Unit or direction vector of this movement
dbNormalizeVector2(1, 1);
}
if(moving) {
//calculating height
float fHeight = dbGetTerrainGroundHeight ( 1, dbObjectPositionX(3), dbObjectPositionZ(3) );
dbPositionObject(3, dbObjectPositionX(3), fHeight + 8.0f, dbObjectPositionZ(3));
//Compute angle
float fTanValue = dbYVector2(1)/dbXVector2(1);
float fAngle = 90.0f - dbAtan(fTanValue);
if(dbXVector2(1) > 0) {
fAngle += 180.0f;
}
//the player should face
dbYRotateObject(3,fAngle);
//move the player
dbMoveObject(3,0.5);
}
The last few code compute the direction the player should face.
Assuming the player is now moving in the direction of V(origin), the question is:
when I click the mouse, instead of driving the player simply with the mouse clicking direction, we let it move in the direction of the vector sum of V(new) and V(origin) for the time duration 100ms. After that, we set the motion of the player to move in the direction of V(new).
As the figure shown, P0 is the mouse clicking position; the green line is the sum of V(new) and V(origin); the red line is the motion path of the player when 100ms expired.
How can I program the code as described above? Better be based on the listed code or any new ideas will be fine.
I'd really appreciate it!!