Sign in to follow this  
armond

Need help in Creating a 2D Serpent, multiple body boss

Recommended Posts

Hi guys I'm working on one of the bosses for my sidescrolling shooting game(2d). And in my game world(this is C++ btw), this is one pointer to a boss object Boss* boss = new AnyBoss(); There are mainly two issues that I have right now 1. How to make the SnakeBoss's body follow the head smoothly. 2. How would the ship collide with the boss's body. To tackle problem #1 1. I have two classes right now, SnakeBoss and SnakeBody. I have a List(vector) of SnakeBody's in class boss. The algorithm for movement is done in the SnakeBoss's move method

SnakeBoss::move(int _x, int _y)
{
    prevX = x;
    prevY = y;

    for (int i = 0; i < MAX_SNAKE_BODIES; i++) //move the bodies
    {
        if (i == 0) 
        {
            snakeBodies[0]->move(prevX, prevY); //move the first body
        }
        else
        {
            sBodyPrevX = snakeBodies[i -1]->getPreX();
            sBodyPrevY = snakeBodies[i -1]->getPreY();

            snakeBodies[i]->move(sBodyPrevX, sBodyPrevY);
        }        

    }

    Sprite::move(_x, _y); //just move the head
}


This is moving the bodies, but in a very stupid way. It doesn't look like the bodies are actually following the head. Problem #2 Since there is only one pointer for both Boss and Ship in my game, I could easily do collision detection on them using bounding boxes.
if ( ship->collides(boss) )
{
    ship->takeDamage();
}

However, this time, my boss has several bodies that I need to test collision with. And I can only do it with one. Changing the single boss pointer to an array or list doesn't seem like a good idea. I'd like to hear your suggestions guys. Thanks!

Share this post


Link to post
Share on other sites
Change the collision code to support multiple collision box enemies, or at least bosses. As for moving the bodies, you could get the same effect you have now by just moving the current tail of the boss to where the head was in the previous frame; I think this is how games used to do it years ago. To get a smoother movement, you can do something like this: move the head first, then for each segment, find the direction to the next segment, and move in that direction such that the distance between segments stays equal to some constant.

diffx = prevbodyx - bodyx
diffy = prevbodyy - bodyy
dist = sqrt(diffx*diffx+diffy*diffy)
diffx = desireddistance * diffx / dist
diffy = desireddistance * diffy / dist
bodyx += diffx
bodyy += diffy

Something like that

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this