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.

I am using the TL game engine I am not allowed to use a physics engine but I need to make the spaceship lean as it corners, I can make it lean but cannot make it return to its starting position. I have looked at implementing some kind of spring physics but I don't understand it.

Here is my code so far

if(myEngine->KeyHeld(Key_A))
    {
        car->RotateY(carSteer * frameTime);
        if(carSteer >= -carMaxSteer)
        {
            carSteer -= carSteerIncrement;
            car->RotateLocalZ(-(carSteer * frameTime));
        }
    }
    if(!myEngine->KeyHeld(Key_A))
    {
        if(carSteer < 0)
        {
            carSteer = 0;
        }
    }
    if(myEngine->KeyHeld(Key_D))
    {
        car->RotateY(carSteer * frameTime);
        if(carSteer <= carMaxSteer)
        {
            carSteer += carSteerIncrement;
            car->RotateLocalZ(-(carSteer * frameTime));
        }
    }
    if(!myEngine->KeyHeld(Key_D))
    {
        if(carSteer > 0)
        {
            carSteer = 0;
        }
    }

All the functions I am calling are built into the engine and I did not write them. Any Help Would Be Appreciated Thanks.

share|improve this question

migrated from gaming.stackexchange.com Apr 4 '12 at 16:40

This question came from our site for passionate videogamers on all platforms.

1 Answer 1

up vote 1 down vote accepted

This may not be the case but I see you are only doing the rotations when holding down a key. In your checks for if you are Not holding the key down, you reset carSteer to 0, but you do not do anything with the rotations to put the spaceship back to a 0 rotation as well.

Based on your comment this does seem to be the issue but you want a way to ease the ship back to its flat orientation as opposed to a quick snap back. You basically want to do the same thing you are doing to get it to tilt but in reverse and based off of a secondary variable so it does not affect your steering as well. NOTE: I only show the changes to one of the key presses. You would want to do the same for the other one as well. (It can use the same tracking variable though)

var carTilt; //assumed to be the same type as carSteer.
if(myEngine->KeyHeld(Key_A))
{
    car->RotateY(carSteer * frameTime);
    if(carSteer >= -carMaxSteer)
    {
        carSteer -= carSteerIncrement;
        carTilt = carSteer;
        car->RotateLocalZ(-(carTilt * frameTime));
    }
}
else // This is a good thing to change as you will not do the logic check twice
{
    if(carSteer < 0)
    {
        carSteer = 0;
    }
    if(carTilt < 0)
    {
        carTilt += carSteerIncrement * 1.5; // Tilt back a little faster
        car->RotateLocalZ((carTilt * frameTime));
    }
}

Now some caveats to this.. I do not have the code and have not tested it. What I do hope I am showing however is that you separate out a measurement of the tilt that is not going to be used for motion. I called it carTilt in this case. You can then decrease this over time instead of instantly and use it to return the car to its 0 orientation smoothly.

Hope this helps.

share|improve this answer
    
The reason for this is a I want a smooth transition but I cannot return to the rotation 0 because it continues rotating at the number you have given it and it I do this you cannot lean the ship at all because they are candled out because there's always one key that isn't pressed. –  bobthemac Apr 4 '12 at 17:30
    
@bobthemac I have added in some more information to address the information in your comment here. –  James Apr 4 '12 at 17:52
    
Thanks for that work perfectly just affected a different part of my movement as the front has to rise but I think I can fix that. –  bobthemac Apr 4 '12 at 19:10

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.