Sign in to follow this  
JorenJoestar

Input-Logic-Physics

Recommended Posts

JorenJoestar    381
Hi guys, I've reached some good results on my graphic engine and now I'm "zooming out" and begin working with other subsystem of my engine. But I found a problem (strange is it?) Reading about game component objects and game logic, I want to develop a good and fast logic system for my demo. Now I have an object (call it entity) that has different parts, reserved for each subsystem. But I messed my head up - I don't know how to do some simple things. Think of a game loop like: - Retrive input; - Update Logic; - Render; Update logic contains (for example) - Rules of the game; - Physics/Collision detection; - AI; - etc now in the case that I press the left key, I control: - if the player can move (rule sof the game, is he petrified???) - if it collide something or I have to swap in - collide - rules? and then...how do you handle different subsystems that access the same informations, like position of the object? thank you guys...sorry to make a great number of question and not to answer and help you on the forum :( great newbie here!

Share this post


Link to post
Share on other sites
BreathOfLife    188
The key that works well for me is layers. One layer of systems handles things like the map, entities, input and whatnot, each subsystem being completely independant from each other. Then on top of that, sits a different layer, which has access to most of the previously mentioned things, and fits them all together, as it has access to everything it needs, object position, type, the map itself ( well, whatever parts of the subsystems you provide external access to, which is nice, nothing is out in the open unless you need it to be ).

Before I devised that model (or discovered rather, Im sure its fairly common, if not very common) I kept running into the problem of cyclic dependancy, which is no fun to try and work around.

Share this post


Link to post
Share on other sites
JorenJoestar    381
Well...let me explain with a simple example...

class Animation
{
public:
int currentFrame;
};

class Player
{
public:
Vector3f position;
bool isMoving; // input was to move...
bool isPetrified;// some magical/logical status on move
bool isBlocked; // the final result: is blocked?
Animation *currentAnim;
PhysixBox *box; // ok, this is ugly!
};


Ok. Now the problem is located in the mainloop:

...

player->isMoving = true; // someone pressed a key to move

...

void mainloop(time)
{
updateLogic(time);
updatePhysic(time);
}

void updateLogic(time)
{
if (player->isMoving && player->isPetrified)
player->isBlocked == true;
}

void updatePhysic(time)
{
if (player->isBlocked)
player->box->setBlocked(true); // I 'disabled' the box for the simulation,
// reducing its forces to 0...

doSimulationStep(time); // update all the scene
}

is this a correct way to handle the logic-physic interaction?
Thank you guys!

Share this post


Link to post
Share on other sites
joe_bubamara    100
A good thing with old-game-loop is it is linear. Your subsystems can only run one at a time, in linear fashion. When one susbsytem is done, then next is using data, and then next etc. So a subsystem will see data that is result of calculations done by last one.

So for a player you gather input, then you update player's position according to user's wish, then you do colision for a player(to test if move is possible, user fall in trap etc), then you do reaction to that collision (possibly physics) which results in end position for your player.

Then you do same thing but with AI to provide input for the rest of CG character (bots and other). This is at least how I do it.

Physics are just a cream on top, to get you more interesting reaction when collisions occure.

When it comes to bots, your "input gather" is AI. AI decides how they will move next frame. After you moved a bot, you do same thing as you did for a player (collision/reaction). Here things can become tricky, since AI can take in observation possible collisions, and outcomes of it and do actions to prevent it, so it might be confusing when working with other entities then human player, what part of the loop one does at the moment(at least for me), but just keep yourself remind of the linearity of game loop to keep it simple.

Share this post


Link to post
Share on other sites
joe_bubamara    100
you are welcome, nice I cleared things upp a bit, btw - that is only how I think of it, mabye other ppl have some other ideas

"good-old-game-loop" was just an expression I use for some very familiar things I like :-) ... nothing to really hang yourself on

Share this post


Link to post
Share on other sites
JorenJoestar    381
ehehehe, I'm only asking how other game do the main loop :D

Looking at quake source, the main loop is done calling the 'think' method on all entities (if I remember well), then in each entity all the calculations are done...

Other ideas?

Another question is: how game logic works here? What if I can't move because of some "logic state" (like in the example, petrified) occurs?

[Edited by - JorenJoestar on May 21, 2008 8:27:08 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this