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-&gt;getPreX();
sBodyPrevY = snakeBodies-&gt;getPreY();
snakeBodies<span style="font-weight:bold;">-&gt;move(sBodyPrevX, sBodyPrevY);
}
}
Sprite::move(_x, _y); <span class="cpp-comment">//just move the head</span>
}
</pre></div><!–ENDSCRIPT–>
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.
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">if</span> ( ship-&gt;collides(boss) )
{
ship-&gt;takeDamage();
}
</pre></div><!–ENDSCRIPT–>
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!
Need help in Creating a 2D Serpent, multiple body boss
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
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement