Matcha
Matcha

Reputation: 141

How would one achieve this "follow another sprite" movement?

In Scratch I'm trying to achieve this "follow another sprite" movement, where the other sprite follows the first sprite's movement exactly, like in this game, Mountain of Faith:

https://www.youtube.com/watch?v=fqHYHOD2-ck

Basically what I'm looking is for the pseudo-code that would help me in both Scratch and JavaScript. I'm not really sure what to do other than that it (obviously) involved the X and Y position of the other sprite.

Thanks.

Upvotes: 1

Views: 1539

Answers (3)

Nerp
Nerp

Reputation: 1

I don't know enough of JavaScript yet but, This sort of thing is made really easy in Scratch.

Point toward Sprite, move (speed of Sprite) steps. Done.

And if that way don't work, Go To Sprite, point in direction of Sprite, turn 180, move distance required, and do this for each link going and pointing to each concurrent following link one behind the other. (in other words go to and point in direction of the last in the line)

This can be done with multiple sprites (easiest way), variables setting temporary positions and stamping, lists and stamping, or you could use clones (which is a little bit more complicated).

Upvotes: 0

Noah Gehlhausen
Noah Gehlhausen

Reputation: 23

This type of problem is one of inverse kinematics. I created a project that does this a while back. https://scratch.mit.edu/projects/250005153/

Essentially, what you want to do is have each successive member of the group move towards the member before it if it is greater than a certain distance away. This distance would be like the radii of circles, for example. This mimics the real life equivalent of a rope or atoms in a chain. Hope this helps!

Upvotes: 1

SaganRitual
SaganRitual

Reputation: 3203

Here's some pseudocode that will get you started. This definitely won't be your final solution; you'll have to play around with the details to get it exactly like you want it. Best of luck to you!

let trackingArrayMaxSize = 100;
let trackingArrayFollowerStart = 50;
var trackingArrayLeaderIndex = 0;
var trackingArrayFollowerIndex = 0;
var trackingArray = [ ];

// Scratch, or whatever game engine you're using, is going to call this once per frame.
// Have a look at the Scratch wiki https://en.scratch-wiki.info/wiki/Game_Loop
// to see how Scratch does it.
function update() {

  ////////////
  // Use the array to track the current position of the main sprite.
  ////////////
  if(trackingArray.number_of_elements < trackingArrayMaxSize) {

    // If the tracking array hasn't reached the max size, then
    // just keep appending our current position to it.
    trackingArray.append(main_sprite_current_position)

  } else {

    // If we've reached the max size, then we start treating the
    // array as a circular array, storing each new position in the
    // next array index as normal, but wrapping back to index 0
    // when we reach the end.
    trackingArray[trackingArrayLeaderIndex] = main_sprite_current_position
    trackingArrayLeaderIndex = (trackingArrayLeaderIndex + 1) % trackingArrayMaxSize
  }

  ////////////
  // Read back from the array to set the position of your following sprite
  ////////////
  if(trackingArray.number_of_elements > trackingArrayFollowersStart) {

    // When the array becomes full enough (experiment to decide how full),
    // set your follower position to wherever the leader was, that many frames before.
    //
    //
    // So say we start with trackingArrayMaxSize = 100 and trackingArrayFollowersStart = 50.
    // At the top of this function each frame, we'll be storing the leader's positions into
    // the array. When the array has 50 elements in it, we come here and set the
    // follower's position to trackingArray[0] -- and up top, we'll be storing the leader's
    // position to trackingArray[50].
    //
    // Next time through, we'll store leader to trackingArray[51], and here we'll be setting the
    // follower's position to trackingArray[1]. So your follower will always be 50 frames
    // behind your leader.
    following_sprite_current_position = trackingArray[trackingArrayFollowerIndex]
    trackingArrayFollowerIndex = (trackingArrayFollowerIndex + 1) % trackingArrayMaxSize
  }

}

Upvotes: 0

Related Questions