• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AhmedCoeia

Snake body movement problem

4 posts in this topic

Hi All, 

I'm trying to simulate snake body movement, the problem is the previous nodes of the snake doesn't follow exactly the head node. 

This is my code to update the nodes

 
for ( int i = 1 ; i < nodes.size(); i++ )
{


Vec2f diff =  nodes[i-1].m_Pos -nodes[i].m_Pos;


float length = diff.length();
Vec2f norm = diff.normalized();


nodes[i].m_Pos+=(length - 8)*norm;


}if ( nodes[0].m_Pos.x < 5 ) 
{
nodes[0].m_Vel.y = -2;
nodes[0].m_Vel.x = 0;


if(nodes[0].counter--<1) 
{
nodes[0].m_Vel.y = 0;
nodes[0].m_Vel.x = -2;
nodes[0].counter = 5;
}


}
 
nodes[0].m_Pos-=nodes[0].m_Vel;

 

Kindly find attached exe file which shows the problem.

0

Share this post


Link to post
Share on other sites

Hi,

 

I realize that this may make me sound like a jerk so let me clarify that that is absolutely not my intention.

 

If you want people to help you debug your code it is essential that you make it as easy for them to understand it as possible.

To that end, you should post a few screenshots depicting the problem you're having. No offense, but I'd rather not download and run any ol' exe file.

 

It would also be helpful if you explained the algorithm you're using to position the segments of the snake's body, as well as including a few comments in the code you posted. 

- why do you subtract 8 from the length when positioning the node?

- what is the purpose of the counter datamember attached to node 0 ?

 

As I understand it, you're using the vector [previous segment -> current segment] to position the current segment? I may be wrong, but I think you'll want to go from head to tail in that scenario. That is, instead of

 

for(int x = 0; x < nodes.size(); ++x)

 

use

 

for(int x = nodes.size() - 1; x >= 0; --x)

Edited by molehill mountaineer
0

Share this post


Link to post
Share on other sites

Hi,

 

I'm really sorry for misunderstanding, English is not my first language. I have commented the code. The problem that the segements of the snake doesn't follow the directions of the first head node, when it bounces to the edges.

// loop thru the nodes

for ( int i = 1 ; i < nodes.size(); i++ )
{

// get the difference between the head and the previous nodes
Vec2f diff =  nodes[i-1].m_Pos -nodes[i].m_Pos;


float length = diff.length();
Vec2f norm = diff.normalized();

// get the normal vector, subtract 8 from the length, so that we have a displacement between the nodes
nodes[i].m_Pos+=(length - 8)*norm;


}
// check the screen left boundary
if ( nodes[0].m_Pos.x < 5 ) 
{
// reverse the velocities
nodes[0].m_Vel.y = -2;
nodes[0].m_Vel.x = 0;

// check how many time elapse to again reverse the velocities of the snake to right direction
if(nodes[0].counter--<1) 
{
nodes[0].m_Vel.y = 0;
nodes[0].m_Vel.x = -2;
nodes[0].counter = 5;
}


}
 // move the head to left
nodes[0].m_Pos-=nodes[0].m_Vel;
0

Share this post


Link to post
Share on other sites

Wouldn't it make more sense to only do movement for the head, and then through a series of previous positions update each node.

as such

for ( int i = 1 ; i < nodes.size(); i++ )
{
    if( i == 0 )
    {
        // Do calculations for new position
        
        //Set position and previous position
        nodes[i].m_PrevPos.x = nodes[i].m_Pos.x;
        nodes[i].m_PrevPos.y = nodes[i].m_Pos.y;
        nodes[i].m_Pos.x = newX;
        nodes[i].m_Pos.y = newX;
    }
    else
    {
        nodes[i].m_PrevPos.x = nodes[i-1].m_Pos.x
        nodes[i].m_PrevPos.y = nodes[i-1].m_Pos.y;
        nodes[i].m_Pos.x = nodes[i-1].m_PrevPos.x;
        nodes[i].m_Pos.y = nodes[i-1].m_PrevPos.y;
    }
}

However, I also saw you have a variable that seems to concern velocity and thus this wouldn't work. This is when you're obviously skipping around by say 32 pixels every step kind of like the old snake games, but with velocity your moving ever so slightly based on how long the gap between each time you run the code.

You could counter it by waiting for a complete step to occur IE if the head is moving at 8 pixels per second, you wait until the head has moved for that second and then update all the nodes to their correct spot, but your head would move effortlessly and the nodes would glitch along, updating theirselves once a second. In that case you could track the previous position up to say 30 steps behind and update the nodes 30 times in that second.

But then in the end... is this the better solution?

0

Share this post


Link to post
Share on other sites

Thanks for the reply,  I didn't get updating the head for 30times in a second and the others for 30 steps in a second, would you show me a pseduocode ?

 

In your code also, how would I set the offsets or the displacement between the nodes ?

Edited by AhmedCoeia
0

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  
Followers 0