Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Snake movement  (Read 2688 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2012-07-12 16:21:37 »

Hello!

I'm looking for some specific behavior for a snake. I'm going to describe it
the best I can. Alternatively, you can look here for similar movement.
http://www.miniclip.com/games/snake/en/

Here are the few premises that this works with.
1. The snake is made of induvidual parts, and it can change the angle the head it heading.
2. Each segment moves at the same pace, all the time.
3. The segments follow eachother, with the head leading.
4. Each segment has a set distance to the ones in front of it, and the one behind it. This distance must be maintained.

I have tried to emulate this behavior. I have tried to have each segment find the
segment in front of it, and point/move towards it. This way, however, they don't maintain the same distance
all the time. If I then explicitly make them wait if the distance is too great, a long snake will only move
it's segments in the front while turning. This is a problem. Avoiding obstacles will be difficult.

Any inputs as to how this can be achieved.

Offline philfrei
« Reply #1 - Posted 2012-07-12 21:05:58 »

Based on what I'm seeing at this game site, I'm thinking the commands to the snake should be in a queue structure (FIFO). Initialize it so that, for starters, every command in the queue is the same (leads directly to the next snake link). When you get a new command, place it in the head of the queue to have it operate on the head of the snake and move all the commands down one position, discarding the last.

Also, have a fixed amount of time between when the commands (all together) are passed down the chain.

Anyway, that is my first guess--that is what I would try.

"It's after the end of the world! Don't you know that yet?"
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #2 - Posted 2012-07-12 23:06:05 »

Based on what I'm seeing at this game site, I'm thinking the commands to the snake should be in a queue structure (FIFO). Initialize it so that, for starters, every command in the queue is the same (leads directly to the next snake link). When you get a new command, place it in the head of the queue to have it operate on the head of the snake and move all the commands down one position, discarding the last.

Also, have a fixed amount of time between when the commands (all together) are passed down the chain.

Anyway, that is my first guess--that is what I would try.

This would work, but it's a difficult implementation. I will give it a go, though. Thanks for your thought.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 13
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2012-07-12 23:24:21 »

Whilst i cant click the link (work blocks it ) so i assume that the snake segments to not change direction/move independantly, rather they inherit the direction from the parent segment.

Surely then you can do what I did back in the day with my snake game and just keep on creating segments at the head and destroying them at the tail for each movement and do not modify the middle segments at all.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2012-07-12 23:33:09 »

Whilst i cant click the link (work blocks it ) so i assume that the snake segments to not change direction/move independantly, rather they inherit the direction from the parent segment.

Surely then you can do what I did back in the day with my snake game and just keep on creating segments at the head and destroying them at the tail for each movement and do not modify the middle segments at all.

It moves smoothly though. Imagine if beads on a string were dragged across a floor. Its close to that.

Offline OttoMeier

Senior Devvie


Medals: 4
Projects: 1



« Reply #5 - Posted 2012-07-18 19:15:48 »

store the past positions from the head. sum the distances to get the position for the rest.

eg.
past position
(1,2),(1,3),(1,4),(1,5)
distance to folwing parts is 2
so you summ up :
(1,2)+(1,3)+(1,4)==>2 so the next position in 1.3 and so on.


Or
Bind the commands to positions on the map. If a snake segment mass that position the command is executed. Probably hard to kepp it deterministic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #6 - Posted 2012-07-18 19:23:14 »

store the past positions from the head. sum the distances to get the position for the rest.

eg.
past position
(1,2),(1,3),(1,4),(1,5)
distance to folwing parts is 2
so you summ up :
(1,2)+(1,3)+(1,4)==>2 so the next position in 1.3 and so on.


Or
Bind the commands to positions on the map. If a snake segment mass that position the command is executed. Probably hard to kepp it deterministic.

That was extremely hard to follow. Could you rephrase, perhaps?

Offline OttoMeier

Senior Devvie


Medals: 4
Projects: 1



« Reply #7 - Posted 2012-07-18 19:52:07 »

Pseudocode:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
     class SnakeHead{
       Vector2 direction;
       Vector2 position;
       double angelspeed;
       ArrayList<Vector2> pastPositions;
    }
   
     class SnakePart{
       Vector2 position;
       double distanceToHead;
    }
     
     class Snake{
        SnakeHead head;
        ArrayList<SnakePart> parts;
    }
     
    void gameloop(long passedtime){
      snake.head.pastPositions.add(snakeHead.position)
      rotateSnakeDirection();
      moveSnakeHead();
     
      double distancetohead=0;
      for (int i=pastPositions.size-1;i>0;i--){
         nextdistancetohead=distancetohead +pastPositions.get(i).distance(pastPositions.get(i-1));
         
         for (SnakePart snakePart: snake.parts){
            if (nextdistancetohead< snakePart.distancetohead <nextdistancetohead){
               snakePart.position.setXY(pastPositions.get(i));
            }
         }
         distancetohead=nextdistancetohead;
      }
     
      if (pastPositions.size>10000){
         pastPositions.delete(0);
      }
    }

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (61 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!