Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

PLOT :

I have been developing a game where the player falls down on Y axis and the camera is a Top-Down view camera. The camera, player (main character in the game) movements etc. have all been implemented based off the idea/notion that the player is falling down Y axis. Hence I used to feed in the vectors based of it. The player is always acted upon gravity.

DESIGN CHANGE :

Now after all these months of developing it, I am changing the design. Now the player who was falling down, upon reaching the ground is going to take off and fly parallel to the ground

PROBLEM :

As I am changing the entire scenario, it creates a complication with vectors I used to feed in for the camera and player. Also due the constraint that the experience should be exactly similar to that off the "falling towards the ground" I have to change the gravity accordingly. Now how do I accomplish this when I have vectors all over the place based off the top down view. Please forgive if I am confusing you here. Give me a min.

To break it into the most primitive segment - I input Vector A, B etc to the camera and the player when the gravity is (0,-1,0). Now if I change the gravity to (1,0,0), how should I change the Vector A, B etc. Is there a way to formulate the Vectors A, B etc when I know the gravity has been shifted from (0,-1, 0) to (1,0,0). All the vectors are normalized. If you ask me the relationship of Vector A, B etc with the gravity (0,-1,0) - I have to say there is no relationship. As it was a game, I supplied the Vectors on how I need the Player and Camera's to work.

CODE :

    // The required Force applied in Flap mode
    void ForceToBeApplied()
    {
            //HORIZONTAL FORCE
            if(!HNEXT)
            {
                    rigidbody.AddRelativeForce((Vector3.right) * DeltaTime * LeftDrag * 3);
                    if(LeftDrag < 400)
                            LeftDrag += ToTarget(DefaultDrag,TurnSine);
                    rigidbody.AddForce(2,(-LeftDrag/50),0);
            }

            if(HNEXT)
            {
                    rigidbody.AddRelativeForce((-Vector3.right) * DeltaTime * RightDrag * 3);
                    if(RightDrag < 400)
                            RightDrag += ToTarget(DefaultDrag, TurnSine);
                    rigidbody.AddForce(-2,(-RightDrag/50),0);
            }

            //VERTICAL FORCE
            if(!VNEXT)
            {
                    rigidbody.AddRelativeForce((Vector3.forward) * DeltaTime * UpDrag * 3);
                    if(UpDrag < 400)  
                            UpDrag += ToTarget(DefaultDrag,TurnSine);
                    rigidbody.AddForce(0, (-UpDrag/50),-2);
            }              
            if(VNEXT)
            {
                    rigidbody.AddRelativeForce((-Vector3.forward) * DeltaTime * DownDrag * 3);
                    if(DownDrag < 400)
                            DownDrag += ToTarget(DefaultDrag, TurnSine);
                    rigidbody.AddForce(0,(-DownDrag/50),2);
            }
    }

IF you happen to look into the above module, I add force to the rigidbody. Those forces were added when the gravity was (0,-1,0). Now if I change my gravity to (say) (1,0,0), how should I change the forces applied to the rigidbody.

TYPE OF APPROACH I THINK : What if I could create a static class which could take those vectors and convert them to equivalent vectors for the current gravitational force.

Simple Diagrammatic Expression of The Problem :

enter image description here

I hope I am clear in the problem I am facing.

Thank you..

share|improve this question
    
Sounds like you've already solved this before. Are you looking for an alternative to refactoring? –  Cameron Fredman Mar 6 '13 at 16:35
    
ahh @CameronFredman no. I have not solved this before. I have been trying for a solution for long time but it is in vain. Just in case I have another refined diagram -> s8.postimage.org/6nibgil7p/Vector_Problem.jpg –  karsnen Mar 6 '13 at 16:40

1 Answer 1

This is an interesting exercise; here is my solution. I built it only thinking about the math, so there may be many sources for error when you put it to use.

Also, it's ability to solve your problem will depend entirely on your own code, and I bet you will have problems. Next time don't forget: vectors can be magic numbers, too :)

The usage is like so:

  1. Every vector that was formerly oriented in the reference frame of A being "Down" now needs to be oriented so that B is "Down".
  2. Put A and B into this static function.
  3. Use the resultant quaternion to tranform every other Vector3 in your code:

See the vector3/quaternion documentation (oh crap, this is XNA, right?)

new Quaternion reorienter = getVectorReorienter(A, B);
Vector3.Transform(changingVector, reorienter, out changingVector);

And also see vector math if you are wondering why these lines do what they do.

private static Quaternion getVectorReorienter(Vector3 A, Vector3 B)
{
    //get and keep input's unit directions
    Vector3 unitA = Vector3.Normalize(A);
    Vector3 unitB = Vector3.Normalize(B);

    //get a unit vector perpidicular to A and B
    Vector3 axis = Vector3.Normalize(Vector3.Cross(unitA, unitB));

    //determine if the angles are >, <, or exactly 90deg. apart
    float cos = Vector3.Dot(unitA, unitB);

    //convert dot product result to an angle
    float angle = (float)Math.Acos(cos);

    //create transformation
    Quaternion reoirienter = Quaternion.CreateFromAxisAngle(axis, angle);

    return reoirienter;
}
share|improve this answer
    
Thank you for answer. I get the logic kind of. But with A and B as your vectors, which would be the base vector. Could you please explain a bit, please? I really need to nail this. –  karsnen Mar 8 '13 at 15:21
    
Sure, I'll edit. –  Seth Battin Mar 8 '13 at 15:32
    
Seth Battin -> Thank you. I hope you understand what I am trying to achieve. I have a reference vector, when this reference moves how should I move the A and B. Just to note A and B has to sort of logically/mathematical relation with the reference vector. –  karsnen Mar 8 '13 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.