Ok, i'll start over :)
I have a player standing in 3D space, and a model is rotating around him, following a circular path (just an ordinary circle). Since that is only a 2D movement, i figured i'd just do 2D calculations, and map those to Vector3 when needed, ignoring the Y component of the 3D space.
The model represents a spaceship, so it needs to face into the correct rotation while doing the circular movement.
| private void UpdateCircleFacingDirection(Vector2 newPos) |
| { |
| Vector2 dir = circleCenter - newPos; |
| dir.Normalize(); |
| float calc = (float)Math.Atan2(dir.X, -dir.Y); |
| |
| rotation = calc + 1.6f; |
| } |
This corrected code from above works. The newPos is the current position along the circular path, the circleCenter is 0,0.
The 1.6f float value is just a value to adjust the base model rotation.
However, i have got a second request. This path is left on random events, and the spaceship flies from its actual position to the opposite position on the circle, and as soon as it reaches this position, it continues the circular path, until the random event occurs again.
Even more, it is not supposed to fly a straight line, but follow a "sine wave" path, to look a bit more dynamic.
The flightpath already works fine, however i am stuck at calculating the facing direction of the model while on the sine wave flight path. This is how far i have got:
| private void UpdateInterceptingFacingDirection(Vector2 newPos, Vector2 sourcePos, Vector2 targetPos) |
| { |
| float distance = Vector2.Distance(sourcePos, targetPos); |
| Vector2 trueDir = targetPos - sourcePos; |
| trueDir.Normalize(); |
| |
| Vector2 center1 = sourcePos + (trueDir * (distance / 4)); |
| Vector2 center2 = sourcePos + ((trueDir * (distance / 4)) * 3); |
| |
| float centerDist1 = Vector2.Distance(newPos, center1); |
| float centerDist2 = Vector2.Distance(newPos, center2); |
| |
| Vector2 deviateDir = newPos - center1; |
| if (centerDist1 > centerDist2) |
| deviateDir = newPos - center2; |
| deviateDir.Normalize(); |
| |
| rotation = ????; |
| } |
The newPos this time is the position of the spaceship along the sine wave path (this already works!), the sourcePos is where it left of the circle, and the target pos is the opposite position, where it is heading to.
As you can see i figured i'd have to split the path into 2 half-circles, and rotate the ship according to this new orientation points. However i am stuck at the last step, getting the correct rotation.
EDIT: here is a link describing the ship paths and the corresponding variables
http://img256.imageshack.us/img256/2623/shippath2.png