Jump to content
  • Advertisement
Sign in to follow this  
punkeskimo

Allegro rotate

This topic is 4484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I just worked out being able to move in any direction in 2d, and now i've run into yet another problem. I have no idea why this doesn't work, i'm trying to rotate the bitmap image as it turns, so it actually looks like it's turning. My code compiles perfectly, and I know the problem isn't drawing the bitmap that's rotating because I tried drawing it offset from the normal bitmap and it draws, just doesn't rotate. I'm going to include my player class and functions, and the logic function with the rotating in it.
//player class stuff and functions
class player_class
{
public:
     float x, y;
     float speed; // players current speed
     float angle; // players angle or direction facing
     
     float turn_speed; // a constant
     float walk_speed; // a constant
     BITMAP *robot;     
     bool boost;
  
   player_class()
    {
       x = 0.0f;
       y = 0.0f;
       angle = 0.0f;
       speed = 0.0f;
       
       turn_speed = .07f;  
       walk_speed = 1.0f;   
    }

void turn_right() { angle += turn_speed; }
void turn_left() { angle -= turn_speed; }
void walk() { speed = walk_speed; }
void stop_walking() { speed = 0.0f; }

void update()
    {
        x += cosf(angle) * speed;
        y += sinf(angle) * speed;
    }
    
};
player_class player;
//rotate function stuff
void rotate() {
    int x = int(player.x);
    int y = int(player.y);
    fixed angle = fixed(player.angle);
}
     
//rotate stuff, everything works but the rotating
void logic() {
     
    int x = int(player.x);
    int y = int(player.y);
    fixed angle = fixed(player.angle);
    
    if (key[KEY_D]) {player.turn_right(); rotate();}
    if (key[KEY_A]) {player.turn_left(); rotate();}
    if (key[KEY_W]) {player.walk();}
    if (!key[KEY_W]) {player.stop_walking();}
    
    player.update();
    
    if (key[KEY_SPACE]) {player.boost = true;}
    if(player.boost == true) {player.walk_speed = 2;}
    if (!key[KEY_SPACE]) {player.boost = false;}
    if(player.boost == false) {player.walk_speed = 1;}
}
//main loop
while(!key[KEY_ESC]) {
    clear_keybuf();
    draw();
    player_class();
    player.turn_right();
    player.turn_left();
    player.walk();
    player.stop_walking();
    rotate();
    logic();
    player.update();
    }

Can anyone help me out?

Share this post


Link to post
Share on other sites
Advertisement
I just glanced at it briefly (gota go to work) but the rotate() function confuses me. You create three local variables, assign values to them, and then the function ends? Ditto for the first three lones of logic().

Share this post


Link to post
Share on other sites
Yea I got a little help with realizing my scopes were all messed up, and have some revised code. It has showed small weird signs of working now, such as blinking in the corner of a rotated picture or showing every rotated form of it on top of the picture lol...

class player_class
{
public:
float x, y;
float speed; // players current speed
float angle; // players angle or direction facing

float turn_speed; // a constant
float walk_speed; // a constant
BITMAP *robot;
bool boost;

player_class()
{
x = 0.0f;
y = 0.0f;
angle = 0.0f;
speed = 0.0f;

turn_speed = .06f;
walk_speed = 1.0f;
}

void turn_right() { angle += turn_speed; }
void turn_left() { angle -= turn_speed; }
void walk() { speed = walk_speed; }
void stop_walking() { speed = 0.0f; }

// ROTATE functions, now these local variables declared are INSIDE the class with the originals of 'em.
void rotate_right() {
int x = int(x);
int y = int(y);
fixed angle = fixed(angle);
int turn_speed = int(turn_speed);
angle += turn_speed;
rotate_sprite(buffer, robot, y, x, angle);
}
void rotate_left() {
int x = int(x);
int y = int(y);
fixed angle = fixed(angle);
int turn_speed = int(turn_speed);
angle -= turn_speed;
rotate_sprite(buffer, robot, x, y, angle);
}

void update()
{
x += cosf(angle) * speed;
y += sinf(angle) * speed;
}

};
player_class player;



void logic() {
//logic of rotating
if (key[KEY_D]) {player.turn_right(); player.rotate_right();}
if (key[KEY_A]) {player.turn_left(); player.rotate_left();}
if (key[KEY_W]) {player.walk();}
if (!key[KEY_W]) {player.stop_walking();}

if (key[KEY_SPACE]) {player.boost = true;}
if(player.boost == true && boostenergy > 0) {player.walk_speed = 2; boostenergy -=1;}
if (!key[KEY_SPACE] || boostenergy <= 0) {player.boost = false;}
if(player.boost == false) {player.walk_speed = 1; boostenergy+=1;}

player.update();
}

void draw() {
int x = int(player.x);
int y = int(player.y);
acquire_screen();
draw_sprite(screen, buffer, 0,0);
draw_sprite(buffer, bg, 0, 0);
draw_sprite(buffer, player.robot, x, y);
release_screen();
}

//main loop
clear_keybuf();
draw();
logic();
player.update();




yea, now all the variables (I THINK) have the right scope that have to do with rotating. Should make a bit more sense.

Share this post


Link to post
Share on other sites
rotate_sprite() takes an angle as a "fixed" number from 0 to 255. You can convert them with itofix(i).

All I see you doing is this: fixed angle = fixed(player.angle);, where player.angle is a float of some sort.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!