Sunday, June 25, 2017

xna 4.0 - How To Approach 360 Degree Snake



I've recently gotten into XNA and must say I love it. As sort of a hello world game I decided to create the classic game "Snake". The 90 degree version was very simple and easy to implement. But as I try to make a version of it that allows 360 degree rotation using left and right arrows, I've come into sort of a problem.


What i'm doing now stems from the 90 degree version: Iterating through each snake body part beginning at the tail, and ending right before the head. This works great when moving every 100 milliseconds. The problem with this is that it makes for a choppy style of gameplay as technically the game progresses at only 6 fps rather than it's potential 60.


I would like to move the snake every game loop. But unfortunately because the snake moves at the rate of it's head's size it goes way too fast. This would mean that the head would need to move at a much smaller increment such as (2, 2) in it's direction rather than what I have now (32, 32). Because I've been working on this game off and on for a couple of weeks while managing school I think that I've been thinking too hard on how to accomplish this. It's probably a simple solution, i'm just not catching it.


Here's some pseudo code for what I've tried based off of what makes sense to me. I can't really think of another way to do it.


for(int i = SnakeLength - 1; i > 0; i--){
current = SnakePart[i], next = SnakePart[i - 1];

current.x = next.x - (current.width * cos(next.angle));
current.y = next.y - (current.height * sin(next.angle));


current.angle = next.angle;
}

SnakeHead.x += cos(SnakeAngle) * SnakeSpeed;
SnakeHead.y += sin(SnakeAngle) * SnakeSpeed;

This produces something like this: Code in Action. As you can see each part always stays behind the head and doesn't make a "Trail" effect.


A perfect example of what i'm going for can be found here: Data Worm. Not the viewport rotation but the trailing effect of the triangles.


Thanks for any help!



Answer




With that algorithm, you're making each segment face the same angle as the one in front of it. That's why they all end up getting directly behind the head, they're ultimately all facing the same angle. As you can see in the Data Worm game you linked, each segment just faces toward the segment in front of it. Determine the angle from the current segment to its next and have the segment point that way instead of setting it to next.angle.


For that implementation, you may want to actually iterate from front to back instead. The behavior will be different based on the direction you pass through the list. Try them both and see which works better.


I've used a similar idea for a project prototype in the past (Unity WebPlayer link). I used similar (albeit 3D) code to get each segment to turn toward the segment in front of it and stay back a set distance. Worked like a charm.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...