Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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 (smooth, not a grid)  (Read 2134 times)
0 Members and 1 Guest are viewing this topic.
Offline sakus

Senior Newbie





« Posted 2012-06-26 17:04:15 »

Hi!

I'm trying to make some sort of a snake game, but I'm having trouble with the movement. I refuse to go the "make the snake move along a grid, placing each part of the snake to the cell the previous part occupied the last update" route as I think it looks stupid, so I started making a smooth moving snake but still with 90 degree turns.

This is working OKish already after the very first try by having each part move independently of each other. Every time the player turns the head, it saves the coordinates and the new direction where the turn happened and each part checks these coordinates of the previous part and does the same change once it hits the said coordinates, again saving the coordinates for the next part to check them etc. Once the part has made the turn, I then fix it's position relative to the part before it to make it line up perfectly as there are some rounding errors etc.

But there are some problems still plus you can just feel that the snake isn't connected but it's rather just several blocks moving next to each other (and when it bugs out the snake snaps in two).

I have two different options here.. either I keep going like this and try to perfect the snake movement so that it a) looks good and b) has no chance of crapping out or I take it one step further and make a freeform moving snake with the head rotating in small degree changes and the body flowing freely (yet the way you'd expect it to do) behind it.

The latter is what I'd like to do, but as I'm not too good at maths I'm having trouble figuring out how to calculate the positions of the body parts.

I came across this which seems interesting to try but I couldn't figure out how to fully translate that into Javanese to see how it works. Again, sucking at maths doesn't help as I can't figure out the corresponding Java tools to use (namely the Vector2.Transform and Matrix.CreateRotationZ in that code (can I use affine transform here somehow?). I'm doing this on libgdx if there are some tools in their built in vector classes and whatnot that I should use.

Any input and help would be appreciated to get me going Smiley
Offline matheus23

JGO Kernel


Medals: 114
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2012-06-26 17:12:26 »

I just wrote down a little helper-class, you could use.
Just use Math.sin / Math.cos for x / y coordinates. Math.toRadians is only used to convert degrees (360 = one turn) to radians (2 * pi = one turn).

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  
public class RotatingMoveable {
   private float x;
   private float y;
   
   public RotatingMoveable(float x, float y) {
      this.x = x;
      this.y = y;
   }
   
   public float getX() {
      return x;
   }
   
   public float getY() {
      return y;
   }
   
   // direction is degrees (0-360, but you can choose -57829547 too ;) )
   public void move(float speed, float direction) {
      x += (float)(Math.sin(Math.toRadians(direction)))*speed;
      y += (float)(Math.cos(Math.toRadians(direction)))*speed;
   }
   
   public void set(float x, float y) {
      this.x = x;
      this.y = y;
   }
}

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sakus

Senior Newbie





« Reply #2 - Posted 2012-06-26 17:18:06 »

Thanks for the reply, that looks like what I'd use to move the head but then how to figure out the correct position for the body parts following the head? I'd need to somehow calculate the correct position for each part based on the location and the rotation of the part before it  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-06-26 17:27:47 »

Can't you still use a grid but interpolate in between for movement?

Offline sakus

Senior Newbie





« Reply #4 - Posted 2012-06-26 17:37:09 »

Can't you still use a grid but interpolate in between for movement?

Hm. I'll have to give that a try for the 90 degree turns based movement, should be better than what I have now, thanks for the idea.
Offline sakus

Senior Newbie





« Reply #5 - Posted 2012-06-26 18:39:18 »

I did some more googling and as a result, this is my current move method (for trying to do a "free roaming" snake).. it doesn't quite work but seeing what it does makes me think I'm on the right track at least.. if nothing else this will hopefully teach me some maths too..


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   public void move(float delta) {
   
      SnakePart snakeHead = parts.get(0);
      int snakeLength = parts.size() - 1;
     
      snakeHead.setPosX((float)(snakeHead.getPosX() - Math.sin(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));
      snakeHead.setPosY((float)(snakeHead.getPosY() + Math.cos(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));
     
      for (int i = 1; i <= snakeLength; i++) {
     
         SnakePart partBefore = parts.get(i-1);
         SnakePart thisPart = parts.get(i);
         
         float xChange = partBefore.getPosX() - thisPart.getPosX();
         float yChange = partBefore.getPosY() - thisPart.getPosY();
         
         float angle = (float)Math.atan2(yChange, xChange);
         
         thisPart.setPosX(partBefore.getPosX() - (float)Math.cos(angle) * parts.size() - 1);
         thisPart.setPosY(partBefore.getPosY() - (float)Math.sin(angle) * parts.size() - 1);
                 
      }
     
   }
Offline sakus

Senior Newbie





« Reply #6 - Posted 2012-06-26 18:51:21 »

I think I got it!

I had misunderstood one of the examples I just googled, this is the final one which seems to work so far.. hopefully it will be of use to someone fighting with the same problem in the future Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
   public void move(float delta) {
   
      SnakePart snakeHead = parts.get(0);
      int snakeLength = parts.size() - 1;
     
      snakeHead.setPosX((float)(snakeHead.getPosX() + Math.sin(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));
      snakeHead.setPosY((float)(snakeHead.getPosY() + Math.cos(Math.toRadians(snakeHead.getRotDegree())) * snakeHead.getSpeed() * delta));
     
      for (int i = 1; i <= snakeLength; i++) {
     
         SnakePart partBefore = parts.get(i-1);
         SnakePart thisPart = parts.get(i);
         
         float xChange = partBefore.getPosX() - thisPart.getPosX();
         float yChange = partBefore.getPosY() - thisPart.getPosY();
         
         float angle = (float)Math.atan2(yChange, xChange);
         
         thisPart.setPosX(partBefore.getPosX() - (float)Math.cos(angle) * 10);
         thisPart.setPosY(partBefore.getPosY() - (float)Math.sin(angle) * 10);
                 
      }
     
   }
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.

rwatson462 (33 views)
2014-12-15 18:26:44

Mr.CodeIt (23 views)
2014-12-15 04:50:38

BurntPizza (51 views)
2014-12-10 07:41:13

BurntPizza (84 views)
2014-12-08 13:46:31

JscottyBieshaar (45 views)
2014-12-05 21:39:02

SHC (59 views)
2014-12-04 01:27:13

CopyableCougar4 (58 views)
2014-11-30 06:32:03

toopeicgaming1999 (123 views)
2014-11-27 00:22:04

toopeicgaming1999 (114 views)
2014-11-27 00:20:36

toopeicgaming1999 (32 views)
2014-11-27 00:20:08
Resources for WIP games
by kpars
2014-12-18 19:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-10 07:35:00

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

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

List of Learning Resources
by SilverTiger
2014-08-06 04:33:27

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

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

List of Learning Resources
by SilverTiger
2014-08-01 01:29:50
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!