0

I am developing a small game on a microprocessor, and I'm trying to get a function which changes the direction a sprite is moving by 90 degrees either left or right dependent on the left button press, or right button press.

Currently this is my code. for changing direction, doesn't perform exactly the task desired..

 void change_direction(Sprite * sprite) {

sprite->x += sprite->dx;//these two lines basically tell the sprite to move in whatever direction the button presses tell it to. by default it moves in a straight line in a northern direction.
sprite->y += sprite->dy;

if ( pressed( SW1 ) ) {
sprite->dx = (sprite->dy) ? -sprite->dy : 1;//this code is very broken, what it does is it moves it either diagonally upwards, or right. it should turn the sprite 90 degrees left everytime the switch is pressed.
sprite->dy = (sprite->dy) ? 0 : -sprite->dx; 
}

else if( pressed( SW0 ) ) {//this code turns the sprite right once from the default direction, but not again. what is should do is turn the sprite 90 degrees everytime.
sprite->dx = -1;
sprite->dy = 0;
}

}

I had an idea of how to approach this; a switch press doesn't change the new direction through changing the dx dy values locally, but instead increments or decrements an integer which controls the direction. I don't know how i would implement such a thing however.

  • Hi sorry, I drew an image to describe the desired movement of the sprite because my wording was strange. It should change 90 degrees every time. http://imgur.com/1Wi3RjP Joachim; yes because it's always moving. LPs; really? x, y is simply the location and dx/dy is the direction it's heading in. – Robert Paulson May 11 '15 at 12:33

2 Answers2

1

Turn right 90 degrees:

tmp = -sprite->dx;
sprite->dx = sprite->dy;
sprite->dy = tmp; 

Turn left 90 degrees:

tmp = sprite->dx; 
sprite->dx = -sprite->dy;
sprite->dy = tmp;

Edit:

If you would like just a temporary shift (while the button is pressed), you need to apply the modifier when updating x/y instead:

if (LEFT_BUTTON_PRESSED) {
    sprite->x -= sprite->dy;
    sprite->y += sprite->dx;
} 
else if (RIGHT_BUTTON_PRESSED) {
    sprite->x += sprite->dy;
    sprite->y -= sprite->dx;
}
else {
    sprite->x += sprite->dx;
    sprite->y += sprite->dy;
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

My solution will be like this

typedef enum 
{
   north,
   south,
   east,
   west
}DIRECTIONS;

typedef struct
{
    uint8_t x;
    uint8_t y;
    uint8_t dx;
    uint8_t dy;
    DIRECTIONS currentDir;
}Sprite;


void change_direction (Sprite * sprite)
{
    sprite->x += sprite->dx; 
    sprite->y += sprite->dy;

    if (pressed (SW0) )
    {
       switch(sprite->currentDir)
       {
          case north:
                     sprite->dx = 1;
                     sprite->dy = 0;
                     break;
          case south:
                     sprite->dx = -1;
                     sprite->dy = 0;
                     break;
          case east:
                     sprite->dx = 0;
                     sprite->dy = 1;
                     break;
          case west:
                     sprite->dx = 0;
                     sprite->dy = -1;
                     break;
       }
   }
}
LPs
  • 16,045
  • 8
  • 30
  • 61