Make a sprite follow a path

Let's say I am working on my own engine using SDL.

I am working on a game that has lily pads moving down a stream and I want the pads to follow a certain path(the flow of the stream). To my surprise, this is actually turning out to be harder than I thought... What would be a good way to implement a simple path for a sprite to follow? Would it be best to just to have a hard coded path ready to do ? (This seems like it would take forever since you would need to code every single location for each frame) or is there a better option for completing this task? I was actually reconsidering my design altogether because of this issue. I was thinking maybe implementing per pixel collision for the edges of the curvy stream and just checking collision against the edges and keeping the pads in the stream this way. This would also take forever but would be much more dynamic.

Any thoughts, ideas, or suggestions would be so awesome!

erock
closed account (2UD8vCM9)
This might sound silly but can you draw up a picture and post up so we can visually see exactly what you're going for?

Not asking for a detailed picture or anything just an idea of how the stream would be shaped and the flow direction.
Last edited on
How about a video?


http://youtu.be/bNH-8S_8Aao

The pads will be coming down the stream, as you can see in the video, the pads do not follow along with the edge.
Last edited on

Really depends on how your lake and grass are defined.

How is your grass and lake defined, is it a static image or defined as map data? - if defined as map data, you could use the left or right edge of the grass as a reference and offset your leaf resulting in the leaf adjusting its horizontal position as necessary.

#                 #######
##               ######## 
###               #######
####                #####
####                 ####
####|offset|@         ###
######               ####
#######             #####
######              #####
####                #####
##                    ###
Static lake

That makes life more difficult for you, or at least time consuming as you would now need to go down the route of creating a path, or even reading the bitmap pixel data (like games such as Worms do as their levels are static so its easier to provide much more detail).

oh okay thanks! This is actually the way I was thinking to do it. I was asking because I was not sure if I were just having a dumb moment and was unable to fathom a better way to do it with a static picture. Right now, I have a very rough path that it follows based on the y positions. Thanks for the feedback.

erock

This may be helpful to you, I came across this a while back which gives an insight into the different methods of collision detection - its a very good and detailed post, well worth a read.

http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/


EDIT, check out Type #3: Bitmask - this is what I was referring to.
Last edited on
oh okay cool! thanks.
@Softrix Again, thanks for the information! Very helpful. I read over the section and it looks like my original "creative" solution was about right in that, when dealing with slopes, it is really better to just get right at the pixel level and handle the collision as so.

I had another creative solution that I wanted to run by you guys that deal with the
http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/ article.

I was thinking I could take time to actually map out the curves of the lake and actually put these points in some sort of linked list (probbaly use a vector). I was thinking I could use the each individual points as a track. I could move the pod along the track and even if the velocities change, each point will still be contained in track list. I would then modify the velocity of the Y coordinate and then search through the list to find the appropriate X coordinate for the track and then move the sprite to this location.

This would be much slower and use a lot of memory but with some thought I think it could easily be optimized, but either way it will use more memory.

What do you guys think of my solution?

Thanks for all the feedback, this community is amazing.

erock
Last edited on
It isn't a terrible solution, and remember that memory is only a concern when you think you might run out of it; otherwise any modern system will have more then you are likely to use in a small game like this. Depending on how you want to object to move in the body of water though then that approach might be more work then it is worth. If you want the object to follow the curve of the water more or less like a log would do IRL then you'll want to describe the rivers curve in a function (remember asking "when will I ever use this?" in math class?), then you use this function to generate the path at some offset in between the two edges of the river.
yup! The solution I had more or less is like a function (from math class, stuff I did not take for granted ;)). I am just predefining each point instead of coming up with the equation. The equation method would use less memory but, like you said does not really matter, just something to keep in mind since I plan on an Android port at some point.

erock
Last edited on
Another thing to consider is that the function method opens the door for you to try your hand at procedural level generation in your next iteration.
oh very true!

erock
Topic archived. No new replies allowed.