views:

560

answers:

4

Hello, I don't know how to implement good 2D grid movement. It is easy to implement movement for units like soldiers, but I don't know how to turn vehicle in elegant way. The movement should be like in C&C Red Alert (not 3, because it's 3D, I want just 2D).

EDIT: I don't ask any code, neither links to pathfinding algorithms, because they're easy to find. But the grid movement isn't. I don't know how "exactly" move from one tile to another, especially with diagonal movement (even though it's "longer" move than orthogonal move).

+1  A: 

one way would be to express the turning radius of the vehicle in grid units per turn, and use a Bresenham circle algorithm to incrementally move them into the next adjacent grid unit on each turn

Steven A. Lowe
+1  A: 

This 4-part series by Eric Lippert should help:

I know you haven't specified C#, but the general algorithm he presents is sound.

Joel Coehoorn
Well, actually I do this in C#, but the point is not pathfinding algorithm (which I pretty much can get anywhere on web). It's about the movement itself.
mnn
+1  A: 

Honestly, I think the best thing you could do would be to reword this question very precisely. When you say that you don't know "how to turn vehicle in elegant way" what does this mean? That you don't know how to draw it? That you don't know some piece of math that you think might be needed? That you want to know how to make it animate smoothly?

I think (and this is just a guess, but an educated one) that the question being vague and your problem are intertwined. If you rewrite your question very specifically, to the exact problem you're having, you'll not only more likely get a useful response, but you may very well solve at least part of your problem.

Beska
+2  A: 

Note, I'm making this up, but it should work fine.

Given a source and destination coordinate, simply "draw a line" between the two points, and move the vehicle along that line.

As the vehicle moves on the more granular graphic surface, you can determine which square the unit is "really" in (via simple coordinate mapping for example).

If it's important a unit be in a single square, then when the unit "stops", it can coast to the next square it can be fully contained it (i.e. if it's already left square A, it continues moving to B even tho the vehicle is "stopped").

Calculate the heading between the two points to determine the direction of the vehicle.

If your vehicle only has 8 directions, then you can point them at the next incremental square destination rather than the final destination. That will look ok for slow moving vehicles, but be kind of funny for fast ones.

Refer to this line drawing algorithm to help calculate the squares. Mind, none of this has anything to do with path finding, of course.

Will Hartung
well, thanks, finally some better answer. I will take look into this. However, I'm giving up on anything simpler than free movement (like in modern 3D RTS games).
mnn
question: will it help (or make it more complicated/worse) when I can choose between 8 directions or 16 directions? I want this approach (instead of freeform movement) because collisions can be easier to detect.
mnn
8 vs 16 directions don't really matter, that's just a matter of resolution for facing when you display the units, you still have to map between "graphic" sprite coordinates to underlying grid coordinates. You collisions should be at the grid level, I think.
Will Hartung
Thanks for help but I'm still hitting dead ends. Wonder if you could post some rough pseudocode.
mnn
http://program.php5.cz/testing.7z - demo program (.NET 2.0)http://program.php5.cz/demo_movement_a.7z - I would like to have this movement (video)Sorry for bumping, but I have a prototype of movement I was able to do (usually works fine). Mind that you can control only 1 vehicle. Still I don't know how to get rid of that stucking (I hope it's not caused by .NET/Drawing/Abstraction). I implemented movement with state machine just like in RTS tutorial on net, where it works without any glitches (of course with DX8).
mnn