XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Moving a model in circular rotation with correction Y rotation

Last post 11/19/2009 5:30 PM by Fjotsee. 7 replies.
  • 11/17/2009 1:42 PM

    Moving a model in circular rotation with correction Y rotation

    Hi everyone
    I am currently stuck on a simple problem. I am moving a model in a circular rotation and need to adjust the facing direction accordingly. Right now i am using this code:

    private void UpdateFacingDirection(Vector2 newPos)  
    {  
        Vector2 normal = Vector2.Transform(newPos, Matrix.CreateRotationZ(MathHelper.ToRadians(90)));  
        normal.Normalize();  
     
        float dotP = Vector2.Dot(normal, Vector2.UnitX);              
        float calc = (float)Math.Acos((double)dotP);  
        this.RotationY = -1.5f + calc;  

    The 1.5f offset is needed to rotate the model into the right facing direction.

    Its working, however when reaching a certain position, it starts to reverse the facing direction, and messing up the rotation.

    Any hints to fix this are welcome...
  • 11/17/2009 2:53 PM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    So you have the model offset somewhere in the XY plane and you want to rotate it around the Z axis?

    Does the newPos variable represent the position of the model?

    What direction are you wanting to have the model face as it rotates?

    Give as much info as possible about what you want to accomplish.
  • 11/17/2009 3:45 PM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    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

  • 11/18/2009 12:34 AM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    Answer
    Reply Quote
    Well my calculus is a bit rusty, but my recollection is this:

    The derivative of a curve function is another function whose solution for a given value of X represents the slope of the original function at that same value of X.

    The derivative of sin(x) is cos(x) as shown here:

    http://en.wikipedia.org/wiki/Sine#Sine

    In other words, if:

    f(x)=sin(x)

    then:

    f'(x)=cos(x)

    If you were actually using sine in your situation, then you could use cosine to find the slope of the sine curve at X (the slope would be in the form of rise/run). This value represents the tangent vector of the sine curve at X. You could then normalize this vector and then rotate it appropriately relative to the orientation of your big circle.

    In your case, all of the above information is really for informational purposes only because in your case, you are not actually using a sine function. You are using two half-circles. In this situation, calculating the tangent vector is even easier.

    The tangent vector for a point on a circle can be calculated by taking the vector from the center of the circle to that point and rotating it 90 degrees.

    Once you have calculated the tangent vector at the point newPos, then you can use this as a basis to generate the ship's matrix. Make sure the vector is normalized.

    Hopefully that gives you enough information to be able to make the proper adjustments to your methods. Personally, I would have written your methods a bit differently by only tracking the current angle for the ship in the big circle and then using a boolean to track whether it is currently traveling on the big circle or is in transit on the half circles. If in transit on the half circles, then you can use the elapsed time to determine where on the half circles to place the ship, based on how fast you want the ship to complete the transit.

    If you still have any questions, let me know.

  • 11/18/2009 1:02 AM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    Answer
    Reply Quote
    One more thought... You don't even really need to track the tangent vector. You can just track the ship's heading angle and update it appropriately relative to the big circle and the half circles. Once the ship is rotated to the correct heading, you can translate it to the correct point in space on either the big circle or the half circles.
  • 11/18/2009 10:05 PM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    Thanks for your elaborate reply, i'll try to understand everything you said and implement parts of it!
    Actually i am using a sine wave for the route, i have been using the half circles for presentation only and as a approximation for the rotation angle on the sine wave track. Which is not accurate, dare i say wrong. Your method sounds reasonable and better, i have currently a lot of code which calculates the paths and the rotations, and porting it to 3d went horribly wrong, my spaceship is turning around like crazy ;) (yes i checked for radions - degrees, somehow i am missing something important somewhere). I'll see what i can come up with myself based on your suggestions, thanks a lot!
  • 11/18/2009 11:03 PM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    Well in my opinion, half circles would be a better choice in your situation because the points where the half circles connect to the big circle would have a smooth tangent transition. If you use sine waves, the connection points would have a more abrubt change in direction. Also, the half circles are easier to implement.
  • 11/19/2009 5:30 PM In reply to

    Re: Moving a model in circular rotation with correction Y rotation

    Some valid points there! To be honest, i have scrapped the movement path earlier and i am using a kind of simpler movement style for the ship. It rotates on a path on a circle, which center rotates on another circle. This time its based purely on 3D, and its a lot easier to do. Also it looks better.A Win-Win Situation, right? :)
Page 1 of 1 (8 items) Previous Next