2
\$\begingroup\$

I have a problem in maths or I do not know. I have drawn few circles that simulates the body of a snake, the head of the snake moves and others should follow. but the is no offset or displacement between the circles, because the current code sets the previous position of the nodes to the current, when I add few numbers to both x, y axis, the body segments get distorted. How do I set the offsets between the segments?

for ( int i = 0 ; 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;

    }
    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;
    }
}

I would like also to check the head's node velocity for example:

 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;
        }

    }
\$\endgroup\$
3
\$\begingroup\$

In fact you don't need to calculate offset for each part in each iterate.

just do it for first part. (How?)

for ( int i = nodes.size()-1 ; i >= 0; --i )
{
    if( i != 0 )
    {
        // Here Do not think about offset. just follow old position of next snake part
        nodes[i].m_Pos.x = nodes[i-1]m_Pos.x ;
        nodes[i].m_Pos.y = nodes[i-1]m_Pos.y ;
    }
    else
    {
        // Do calculations for new position "with a proper offset".
        // Is your question about how to calculate this offset ?
        nodes[0].m_Pos.x = .. ;// calculated position
        nodes[0].m_Pos.y = .. ;// calculated position
    }
}

About New head's position calculation;

Assume current head's position is (x,y). so new position simply can be calculate like this:

enter image description here

and more specific :

enter image description here

\$\endgroup\$
15
  • \$\begingroup\$ Yea, I would like to calculate the offset, I do not want the circles to overlap on each others due to the copying of the previous positions into the current positions. Furthermore, I'm trying to play with the head's velocity up and down. \$\endgroup\$
    – Mahmoud
    Aug 27 '13 at 7:17
  • \$\begingroup\$ You haven't mentioned this in question. ok \$\endgroup\$
    – Emadpres
    Aug 27 '13 at 7:20
  • \$\begingroup\$ Thanks, so when I change the velocity of the head, I must check if its downwards or upwards directional, and then recalculate the offsets ? \$\endgroup\$
    – Mahmoud
    Aug 27 '13 at 7:39
  • \$\begingroup\$ Yes. It must be clear how to calculate next position according to x/y velocity of snake. you also can handle cross-direction ( for xVelocity=+1 and yVelocity=+1 ). If you need more clarity, I can explain more. but I think you got it. :) \$\endgroup\$
    – Emadpres
    Aug 27 '13 at 7:48
  • \$\begingroup\$ I need more clarity, If you can, I stayed much to get it :) \$\endgroup\$
    – Mahmoud
    Aug 27 '13 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.