Results 1 to 5 of 5
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default looking for 'trail' object

    Hello,

    I'm looking for a class in the Java APIs that will help me with the following:

    I'm programming a game. In the game, I have something like a train or a centipede. It's basically a chain of objects that moves around like a snake. What I'm looking for is something like a trail or path, something that can be traced or iterated. The head element in the chain would create the trail as it moves along, the middle elements would simply follow the trail, and the last element would delete the trail as it passes over it.

    I'm looking for this trail object so that I have a way to determine how the elements in the chain (except the head) will move (i.e. they'd just follow the trail). Is there such a thing?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Two ways come to my mind: if all segments are the same, then simply add a new "head" to the current head and remove the tail with each tick of the clock. If segments are different, then move the head segment and then move each subsequent segment to the spot held by the one ahead of it.

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Yeah, those would work except that I'd like the animation to be smooth and continuous - so every element in the chain moves pixel by pixel. That means that you can't just plop each chain element into the position of the one ahead of them. Following a smooth and continuous path, however, would do the trick.

    The work-around I'm thinking of if there is no such class in the Java APIs, is to give each chain element a target (a Point object) that is the X, Y coordinates of the element ahead of it. The animation then consists of each element moving directly towards its target. Once it reaches its target, a new target is set, again according to the X, Y coordinates of the one ahead of it.

    I just found that programming this is a lot more tedious than I anticipated, so I want to make sure I'm not overlooking certain available tools in the Java APIs expressly for this purpose.

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    I would just use an Array. The head goes to point x, then it sends that point x to the move methode of the next segment. x-is stored at the last position in an array in that methode, every point in the array is moved one place down untill you reach 0. The item that was previously at place 0 is the place the segment moves to and that point is send into the move methode of the next segment. The length of the array determines the distance between the segments and the last segment instead of sending the position it went to, just signals one "move" of the whole snake is over.

  5. #5
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks everyone for your advice. I think I'm going to continue to inquire about this on another forum, just to see if there are any other responses.

    Needless to say, I'll be cross-posting: Java 2D - looking for 'trail' object

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  2. Replies: 2
    Last Post: 01-29-2010, 02:01 AM
  3. Concurrent trail example
    By bert682 in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-11-2009, 04:47 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Replies: 1
    Last Post: 07-22-2008, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •