views:

34

answers:

2

I am implementing a turn based game, there are two sides and each side has several units, at each specific moment only one unit can move across the board.

Since only one unit can move at a time, after i figure out where it should go, as far as the simulation is concerned it Can instantly be teleported there, but playing the game you would want to see the unit moving so that you realise who moved and where he went.

The question is, would you put the movement algorithm (eg interpolating between 2 points in N seconds) in the model and then have the view show the unit in the interpolated position without even knowing that it is moving, or teleport the unit and notify the view that it should show the unit moving as best as it wants.

If you would take the second approach, how would you keep the simulation from running too far ahead of the view, would you put the view in command of resuming the simulation after the movement ended?

Thanks in advance, Xtapodi.

A: 

What you probably want is to have the unit image move each frame. How far to move the image each frame is similar to your interpolation.

unitsPerSecond = totalUnits / (framesPerSecond * totalSeconds)

So if I want to move an image from position 0 to position 60 in 2 seconds and my framerate is 30, I need to move 60 units in 60 frames, therefore my speed is 1. So each frame, I move the image 1 unit, and if moving the unit will take me beyond my destination, simply set my location to my destination.

Rob Elliott
Thank you Rob Elliott for you quick response. The question is not how to interpolate or make sure it is on the corrent position in the end. The question is if the interpolation is in the model or in the view and the resulting time synchronization problem between the model and the view in the later case.
Xtapodi
+2  A: 

Ah, yet another example that reminds us that MVC was never originally designed for real-time graphics. ;)

I would store the current position and the previous position in the model. When the object moves, the current position is copied into the previous position, the new position is copied into the current position, and a notification is sent to the view that the model has changed. The view can then interpolate between the old and the new position accordingly. It can speed up, slow down, or even remove the interpolation entirely based on the specific view settings, without requiring any extra data to be stored within the model.

Rather than storing the current position and the previous position, you could instead just store the last move with each unit, and the move itself contains the previous position. This is probably more versatile if you ever need to store extra information about a move.

Kylotan
Thank you Kylotan. Yes i think i would go for your second suggestion. So i take it that you would opt for puting the movement entirely on the view and keep the model simple, if you were to implement both the model and the view yourself?
Xtapodi
Yes. The logical movement in terms of the game rules is merely from one place to another with no intermediate motion, so that's what I'd store on the model. The physical movement between those places is just a presentation detail. If in doubt about what to put in the model and what to put in the view, ask yourself how you would turn it into a play-by-mail game, or a text-based game.
Kylotan
Thank you Kylotan that helped me guide my train of thought :)
Xtapodi