User controlled sprite on a pre-determined path.

Started by
2 comments, last by Zeraan 14 years, 1 month ago
Before I go on I'll tell you where I'm coming from... I'm working on a few concept ideas using the cocos2d Game Engine for iPhone using Objective-C. My need is for a sprite (train) to move a long a pre-defined path (tracks). And I'm a little unsure of how I could go about doing this. My initial idea was making a class that would hold pre-made action sequences for each type of track piece the train is approaching. Though one of the many problems I discovered was if I wanted to change the speed of the train I would need to change the duration of the action, and I can't change the action duration while the action is in the process of being ran. So the speed wouldn't change until the train reaches the next piece of track... Another idea was have a method being called constantly a set rate (depending on the speed) that would move the train so far in a given direction along the path. This would be easy if the track was simply a straight track, but handling pre-determined path that would be curved at times I'm not sure about. The benefit of the previous idea was cocos2d has a bezier feature for actions that involve moving a sprite along a curved path. Anyone have any suggestions and input on how I could go about handling a user controlled sprite (train) that is constrained to a path (tracks)? Any help is appreciated! If you need further information feel free to ask.
Advertisement
I would define your track as a B-spline (Bézier curve). It's pretty easy to parametrically determine where you should be on the track given your current speed and time elapsed.

As for how to do it:

Option 1 is create an update loop where you pass delta time, you can attach this to the appRender tick (name is probably off, but it's the main render method created when you create a new OpenGL ES iPhone project). By default OpenGL ES projects will try to call your render method 60 times a second.

Option 2, if you're not using OpenGL, the iPhone provides various timer classes, search the online SDK reference to find example projects. But essentially they will ping you every predetermined interval. You can use that for your delta time update loop body, basically, inside of which you can reposition your sprite and force the view to redraw.

To make your life easier make the "predetermined path" just "move from right to left of screen". Then you can skip learning b-spline math and get an animation system working. Once you have that done, it's just a matter of changing the logic of where to move.

-me
Quote:Original post by Palidine
I would define your track as a B-spline (Bézier curve). It's pretty easy to parametrically determine where you should be on the track given your current speed and time elapsed.

As for how to do it:

Option 1 is create an update loop where you pass delta time, you can attach this to the appRender tick (name is probably off, but it's the main render method created when you create a new OpenGL ES iPhone project). By default OpenGL ES projects will try to call your render method 60 times a second.

Option 2, if you're not using OpenGL, the iPhone provides various timer classes, search the online SDK reference to find example projects. But essentially they will ping you every predetermined interval. You can use that for your delta time update loop body, basically, inside of which you can reposition your sprite and force the view to redraw.

To make your life easier make the "predetermined path" just "move from right to left of screen". Then you can skip learning b-spline math and get an animation system working. Once you have that done, it's just a matter of changing the logic of where to move.

-me


I'm sort of new to game programing, only made and released 1 previous game. So sorry if I'm a little "slow" or have a bit of trouble.

Referring to option 2 cocos2d has a timer class that could be used for this. The re-positioning is also handled easily by cocos2d, the problem is determining the reposition of the train (mainly the curve).

If it was simply a straight section of track that is horizontal I would just reposition the sprite's x position in a given direction, or vertical straight track the y position (The game would be a 2d game with an aerial view). It's the curve section of track that I'm still unsure how to handle.
Palidine gave the solution to moving along a curve, he suggested that you use B-Spline.

If you plug in time and speed, it will give you the x,y position. Speed could be increments (you can increase or decrease it to move forward or backward if free moving), or a constant speed based on time for movement between nodes.

At least that's what I understand from B-Splines, never used them before.

This topic is closed to new replies.

Advertisement