14 Replies - 676 Views - Last Post: 01 May 2013 - 09:44 PM Rate Topic: -----

#1 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 05:16 AM

Hey everyone:

So my question is pretty simple... but getting it to work like that is another story. So I'll dig right in!

Objective: I'm trying to make a snake like game and I'm having issues with the trail following the path. I've used previous methods that DID work, however, I'm trying to eliminate the "destination" object so the snake has a free move feel instead of a grid feel.

My issue is when I started rewriting my player class, I got rid of a lot of redundant stuff. My final task is to fix how the trail moves. I'm wanting the trail to follow its trail by sliding into position.

The code I have written that works before with the destination(Grid-feel -- based on 20 px movment ):

public void Move(int delta){
 //        double moveSpeed = (delta * .6f) - (oreWeight * ore + armorWeight * armor);

        //Smooth animation for snake movement
        if(IsMoving() && (X.equals(destX) && Y.equals(destY)) ){
            for(int i = 1; i < getSize(); i++){
                if(destX.get(i).intValue() != X.get(i-1).intValue()){
                    destX.set(i, X.get(i-1));
                }
                else if(destY.get(i).intValue() != Y.get(i-1).intValue()){
                    destY.set(i, Y.get(i-1));
                }
            }

            if(left){
                destX.set(0, destX.get(0) - GameBoardNew.blockSize);
            }
            else if(right){
                destX.set(0, destX.get(0) + GameBoardNew.blockSize);
            }
            else if(up){
                destY.set(0, destY.get(0) - GameBoardNew.blockSize);
            }
            else if(down){
                destY.set(0, destY.get(0) + GameBoardNew.blockSize);
            }
        }
        else{
            for(int i = 0; i < getSize(); i++){
                if(up || down){
                    if(X.get(i).intValue() != destX.get(i).intValue()){
                        X.set(i, X.get(i) + Integer.signum(destX.get(i) - X.get(i)));
                    }
                    else if(Y.get(i).intValue() != destY.get(i).intValue()){
                        Y.set(i, Y.get(i) + Integer.signum(destY.get(i) - Y.get(i)));
                    }
                }
                if(right || left){
                    if(Y.get(i).intValue() != destY.get(i).intValue()){
                        Y.set(i, Y.get(i) + Integer.signum(destY.get(i) - Y.get(i)));
                    }
                    else if(X.get(i).intValue() != destX.get(i).intValue()){
                        X.set(i, X.get(i) + Integer.signum(destX.get(i) - X.get(i)));
                    }
                }
            }
        }
}



And now, since I have reworked it, I am adding in a velocity of how fast the player is traveling along with other conditions, along with trying to get rid of the destination part of the formula and using its body as a way to figure that out. However, what I have, works, until the player moves around fast in different directions... then the body just clumps up together. Its essentially the same formula, but still very puzzled.


New code eleminating Grid-feel by using the moveSpeed variable.

public void Move(int delta){
 //        float moveSpeed = (playerSpeed - (oreWeight * getSize() + armorWeight * armor)) * delta;
    float moveSpeed = 1f;

    if(IsMoving() && player.get(0).getX() == playerDest.get(0).getX()){
        for(int i = 1; i < getSize(); i++){
            if(playerDest.get(i).getX() != player.get(i - 1).getX()){
                playerDest.get(i).setX(player.get(i - 1).getX());
            }
            else{
                playerDest.get(i).setY(player.get(i - 1).getY());
            }
        }

        if(left){
            playerDest.get(0).setX(player.get(0).getX() - moveSpeed);
        }
        else if(right){
            playerDest.get(0).setX(player.get(0).getX() + moveSpeed);
        }
        else if(up){
            playerDest.get(0).setY(player.get(0).getY() - moveSpeed);
        }
        else if(down){
            playerDest.get(0).setY(player.get(0).getY() + moveSpeed);
        }
    }

    for(int i = 0; i < getSize(); i++){
        Rectangle location = player.get(i);
        Rectangle inFront = playerDest.get(i);
        int xVel = Integer.signum((int)inFront.getX() - (int)location.getX());
        int yVel = Integer.signum((int)inFront.getY() - (int)location.getY());
        if(up || down){
            if(location.getX() != inFront.getX()){
                if(xVel == 1){
                    location.setX(location.getX() + moveSpeed);
                }
                else if(xVel == -1){
                    location.setX(location.getX() - moveSpeed);
                }
            }
            else if(location.getY() != inFront.getY()){
                if(yVel == 1){
                    location.setY(location.getY() + moveSpeed);
                }
                else if(yVel == -1){
                    location.setY(location.getY() - moveSpeed);
                }
            }
        }
        else if(right || left){
            if(location.getY() != inFront.getY()){
                if(yVel == 1){
                    location.setY(location.getY() + moveSpeed);
                }
                else if(yVel == -1){
                    location.setY(location.getY() - moveSpeed);
                }
            }
            else if(location.getX() != inFront.getX()){
                if(xVel == 1){
                    location.setX(location.getX() + moveSpeed);
                }
                else if(xVel == -1){
                    location.setX(location.getX() - moveSpeed);
                }
            }
        }
    }
}



I just don't understand how this is not working without the "destination" object. To me that seems a little redundant. Any help would be greatly appreciated!

If I do need to clarify any information, I will be glad; just ask!

Thank you for taking your time to read this!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem moving a trail on a path in Snake-like game

#2 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 05:34 AM

Sorry guys, this is the actual code for the second code block:


public void Move(int delta){
//        float moveSpeed = (playerSpeed - (oreWeight * getSize() + armorWeight * armor)) * delta;
        float moveSpeed = 1f;
            
        if(IsMoving()){
            for(int i = 1; i < getSize(); i++){
                Rectangle location = player.get(i);
                Rectangle inFront = player.get(i - 1);
                int xVel = Integer.signum((int)inFront.getX() - (int)location.getX());
                int yVel = Integer.signum((int)inFront.getY() - (int)location.getY());
                if(up || down){
//                    if(xVel != 0){
                    if(location.getX() != inFront.getX()){
                       if(xVel == 1){
                            location.setX(location.getX() + moveSpeed);
                        }
                        else if(xVel == -1){
                            location.setX(location.getX() - moveSpeed);
                        }  
                    }
//                    if(yVel != 0){
                    else if(location.getY() != inFront.getY()){
                        if(yVel == 1){
                            location.setY(location.getY() + moveSpeed);
                        }
                        else if(yVel == -1){
                            location.setY(location.getY() - moveSpeed);
                        }
                    }
                }
                else if(right || left){
//                    if(yVel != 0){
                    if(location.getY() != inFront.getY()){
                        if(yVel == 1){
                            location.setY(location.getY() + moveSpeed);
                        }
                        else if(yVel == -1){
                            location.setY(location.getY() - moveSpeed);
                        }
                    }
//                    else if(xVel != 0){
                    else if(location.getX() != inFront.getX()){
                        if(xVel == 1){
                            location.setX(location.getX() + moveSpeed);
                        }
                        else if(xVel == -1){
                            location.setX(location.getX() - moveSpeed);
                        }
                    }
                }
            }
            
            if(left){
                player.get(0).setX(player.get(0).getX() - moveSpeed);
            }
            else if(right){
                player.get(0).setX(player.get(0).getX() + moveSpeed);
            }
            else if(up){
                player.get(0).setY(player.get(0).getY() - moveSpeed);
            }
            else if(down){
                player.get(0).setY(player.get(0).getY() + moveSpeed);
            }
        }
    }


This post has been edited by Twister1002: 01 May 2013 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 06:15 AM

I'm guessing that left, right, up, and down are boolean values. Is only one of them true at any given time? Because this code assumes that that is the case.

Many of the conditions in the if-else-if statements are incompatible, and can be true or false, independent of the state of the other. As a result, the second statement may not evaluate, regardless of whether or not the condition evaluates to true.

ORACLE said:



//These are incompatible statements. The left hand sides should be the same object.
if(location.getY() != inFront.getY())
    continue;
    //If this is true, it skips the else-if clause -- even though the else-if clause may also be true.
else if(location.getX() != inFront.getX())
    continue
    //If location.getY() != inFront.getY(), this will not evaluate -- but not because location.getX() == inFront.getX()


This post has been edited by Gungnir: 01 May 2013 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 06:48 AM

Yes, only one movement is allowed as true: up, down, left, or right.

Ooh wow... Didn't know that. Then why does it do it in debug mode? I also have multiple nested if statements... Nothing seems to be wrong with those.
Was This Post Helpful? 0
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 07:39 AM

Nested if statements are fine. There's nothing wrong with them -- I use them, everybody uses them. Just know that each statement in the block restricts access to all other statements in that block.

Also, the left hand side of a condition should never change during an if-then-else statement.

Can the player move on the x AND y axis at the same time? Or just one of them? I'm not seeing any angular movement code here.

This post has been edited by Gungnir: 01 May 2013 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 07:59 AM

Ahh, ok. I will keep that in mind, and move my arguments around.

The player can only move on one axis at a time(either X or Y, not X and Y), until a new direction is selected.
Was This Post Helpful? 0
  • +
  • -

#7 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 08:38 AM

I wrote this code for an old project that had movement similar to what you've described. I've simplified it a lot so that it's closer to your specific needs. Note that you can't just copy and paste it (you probably know that already, but I don't want you to try and then get annoyed at me). It's just an analogue for how I would tackle your issue.

All of this was in the hero class.

	//method to set the destination, and save the current position just in case something goes wrong.
	public void move(int x, int y)
	{
		this.lastx = this.x;
		this.lasty = this.y;
		this.moveToX = x;
		this.moveToY = y;
	}
	//This is where the actual movement occurred
	private boolean animatemovement(int x, int y)
	{
		//If player is at destination --
		if(this.x == this.moveToX && this.y == this.moveToY)
		{
			moving = false;
			xSpeed = 0;
			ySpeed = 0;
		}
		//else --
		else
		{
			moving = true;
			if(this.x == this.moveToX)
			{
				//... Move Y
				//If the destination is above the player
				if(this.y - this.moveToY > 0)
					//move up
					ySpeed = - Screen.yVel;
				//else move down
				else if(this.y - this.moveToY < 0)
					ySpeed = Screen.yVel;
				this.y += ySpeed;
			}
			else
			{
				//... Move X
				if(this.x - this.moveToX > 0)
					xSpeed = - Screen.xVel;
				else if(this.x - this.moveToX < 0)
					xSpeed = Screen.xVel;
				this.x += xSpeed;
			}
		}
		return moving;
	}
	//update() was called every frame. If the player wasn't at the destination, he would move to the destination
	private void update()
	{
		animatemovement(this.moveToX, this.moveToY);
	}


Was This Post Helpful? 0
  • +
  • -

#8 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 07:08 PM

I understand how to move the head piece, that isn't the issue. I'm trying to get the trail to follow its path, but it likes to clump up instead.
Was This Post Helpful? 0
  • +
  • -

#9 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 07:47 PM

I think I get it.
You might be able to make a queue of points that the trail follows. It's important to note the benefits of such a method. The trail can get longer/shorter, like in a traditional game of Snake, and you can test whether or not any of these entities collides with the head.
//looped during game loop, when each entity has completed its movement
LinkedList<Point> pointQueue = new LinkedList<Point>();

pointQueue.clear();

pointQueue.add(new Point(location.getX(), location.getY()));
for(trailEntity trail : trailList)
    pointQueue.add(new Point(trail.getX(), trail.getY()));
//pop off the last element, it's not needed
pointQueue.remove(pointQueue.size() - 1);

//then move the trail entities to their next point.
for(int i = 0; i < trailEntity.size(); i++)
{
    trailEntity.get(i).moveToPoint(pointQueue.get(i));
}



So hopefully, you'll have something like:
H = head, T = trail, 0 = empty

//0000000000000000000000
//000TTTTTTTTTTTTH000000
//0000000000000000000000


//000000000000000H000000
//0000TTTTTTTTTTTT000000
//0000000000000000000000


//000000000000000TH00000
//00000TTTTTTTTTTT000000
//0000000000000000000000


//000000000000000TTH0000
//000000TTTTTTTTTT000000
//0000000000000000000000


This post has been edited by Gungnir: 01 May 2013 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 08:22 PM

So in other words, I still need a have a destination variable to do what I want to do?
Was This Post Helpful? 0
  • +
  • -

#11 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 08:28 PM

Of course. You can't just have your entities wonder aimlessly, they have to follow eachother -- and the definition of following something is moving to its previous position. So, unless you want to create a glitchy smart-pathing system (which I wouldn't recommend), this is the way to go.

Is there a reason why you don't want to have a LinkedList of destination Point(s)?
Was This Post Helpful? 1
  • +
  • -

#12 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 09:11 PM

Ahh ok. No there isn't a really good reason why I don't want a destination object. I was just trying to cut back on the amount of code I had. I was also hoping that I could get it to work without having to use one. Just a little challenge I wanted to do =).

Thank you for your help ^_^. I'm going to readd the destination object and just keep going on!
Was This Post Helpful? 0
  • +
  • -

#13 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 09:25 PM

Instead of cutting back on your code try modularizing and encapsulating it.

If a movement method is designed for your Head, put it in the Head class. If much of the same stuff is happening in each IF statement block, try creating a method for that. In my code I had a method to request a movement, a method to carry out a movement, and a method to check whether or not the hero was moving.

That's taking it a little bit far, I'll admit, but the purpose of shortening your code is to make it easier to manage, not harder to manage. :P

Good luck with your game, it looks neat!
Was This Post Helpful? 0
  • +
  • -

#14 Twister1002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 08-April 12

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 09:29 PM

This is gonna sound bad, but I never understood what modularizing, serializing, and encapsulating meant. Just sounds like fancy terms. Haha. (Research time).
Was This Post Helpful? 0
  • +
  • -

#15 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Problem moving a trail on a path in Snake-like game

Posted 01 May 2013 - 09:44 PM

Separate entities into classes.
Separate code into methods*.

*Functions that are Class members.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1