PDA

View Full Version : Moving direction influence - how?


ZeHa
04-23-2007, 09:19 AM
Hello,

at the moment, I'm planning a flexible game engine that serves my needs and can be scripted. That way I hope to create future games more easily with my own tools, trying not to touch the engine code itself too frequently.

But now I've got a little design problem, because I'm not sure about the proper way to implement it. And I want it to be made properly, as I want a clean engine, and I don't want to hack around (yet :D ).

Okay, enough introduction :) now the technical part:
Imagine you play a tile-based game, and you walk around some static floor tiles, but then you cross an "assembly line" that influences your moving direction. Now the influence direction should be added to your normal moving direction that you can control with the keyboard.

I use a vector for that, so my idea would be to give the Entity class a "normal direction" (or concious direction or call it whatever you wish :) ), and an "external influence direction". When the Entity's position would be calculated for rendering or collision check, both directions would be simply added.

But would that be the "correct way"? I never did something like that, I only had ONE direction variable and now there would be two, so it feels a little uncomfortable for me on first sight.

Are there any other possibilities, that I didn't think of yet, but have "superior correctness"? :)

electronicStar
04-23-2007, 09:35 AM
That shouldn't be a problem , you just increment position according to player input then you also increment it according to external influence.
Both movements should add up which is normal (you move faster if you walk on a conveyor belt and in the same direction)

Or maybe I didn't understand the question:confused:

MFS
04-23-2007, 09:38 AM
I think that this method would work fine, although I typically only use one vector for each entity.

In this instance, if you want user input to affect the entities movement then you simply add to the vector according to the respective input. In the same way, you would just add the 'belts' vector to the entities vector.

A quick example: In the input function, the user presses 'left' adding the (-1.0,0.0) vector to the entity's vector. In the entity's update, it turns out the entity is on the conveyor belt as well, adding its vector (0.0,1.0) to the entity's. So when it comes time for the entity to actually update its position, its total movement vector is (-1.0,1.0) thus offsetting X in the negative direction and Y in the positive.

Does this help?

Mark Sheeky
04-23-2007, 09:39 AM
Sometimes a character could have motion controlled by multiple vectors... thrust and gravity for example, or a gust of wind. I think it's better to store those multiple vectors as you suggest instead of trying to add them when the influence occurs. It depends on context of the game. Gravity usually applies equally to all objects, but not the wind. Just my thoughts.

Mark

ZeHa
04-23-2007, 10:02 AM
Ahhh well I forgot to mention something. I don't increment the position in every frame, rather I only store the last time the direction changed and the position at this time. Now when I draw or check collisions, I call a getXPos(time) function which calculates the current position by the timestamp given as an argument. That way I'm able to recreate certain situations by using an older timestamp or even looking ahead.

So it's not possible to just add both in each frame. Instead, I have to record every direction change, whether by controlling the player or from outer influences.