Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

878477 Posts in 32924 Topics- by 24336 Members - Latest Member: BeefJack

May 22, 2013, 01:14:32 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Update sprite rotation perpendicular to its trajectory
Pages: [1]
Print
Author Topic: Update sprite rotation perpendicular to its trajectory  (Read 412 times)
akademiks
Level 0
***


View Profile
« on: January 04, 2012, 12:54:44 PM »

Hi,

I implemented an orbital rotation code for my ship sprite around a given planet but i don't remember how to update its rotation so its always perpendicular to its trajectory...

Any tips?
Logged
Netsu
Level 10
*****



View Profile WWW
« Reply #1 on: January 04, 2012, 01:12:08 PM »

Something like this should help (assuming your rotation is in radians and you're using a velocity vector):

Code:
rotaton = atan(vel.y, vel.x);
Logged

Polly
Level 2
**


View Profile
« Reply #2 on: January 04, 2012, 01:54:36 PM »

The solution Netsu posted works .. but depending on your game / requirements you might be able to "plug" the variable that drives your orbit directly into the sprite rotation. Check out the following example ( source + exe )

http://bayfiles.com/file/1jDX/bPD4TR/Orbit.zip
« Last Edit: January 04, 2012, 02:03:31 PM by Polly » Logged
akademiks
Level 0
***


View Profile
« Reply #3 on: January 05, 2012, 06:53:35 AM »

Thanks guys,

Hey Polly is it possible to provide me the source without the bayfiles.com overhead?

Thx
Logged
akademiks
Level 0
***


View Profile
« Reply #4 on: January 10, 2012, 05:05:47 AM »

Finally i found the solution to my problem...

I was not getting the fact that when my sprite has 0 rotation, it means that it has 90 degree of rotation when looking at the horizontal axe
This is because my sprite is a ship, pointing to the top of the screen by default.

Code:
- (void) orbit: (float) dt
{
    float radianToDegree = 180.0f / M_PI;
    float degreeToRadian = M_PI / 180.0f;
    
    float dir = -1.0f;
    float angularSpeed =  120.0f * degreeToRadian * dt;
    
    currentOrbitalRotation += angularSpeed * dir;
    
    if (currentOrbitalRotation > 2 * M_PI) {
        currentOrbitalRotation -= 2 * M_PI;
    }
    else if(currentOrbitalRotation < 0.0f)
    {
        currentOrbitalRotation += 2 * M_PI;
    }
    
    CGPoint newPos = CGPointMake([planet position].x + [planet planetRadius] * cosf(currentOrbitalRotation),
                                 [planet position].y + [planet planetRadius] * sinf(currentOrbitalRotation));
    
    // Multiply by -1.0f because the rotation is the inverse of the orbitation angle
    // + 180.0f because the direction is upside down (clockwise) (cocos2d's rotation goes from 0 -> 180 & -180 -> 0)
    [self setRotation:currentOrbitalRotation * -1.0f * radianToDegree + 180.0f];
    [self setPosition:newPos];
}
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic