Sign in to follow this  
AhmedCoeia

Programming a game play Essentials

Recommended Posts

Hi All,

 

I have a decent knowledge of c++, java,..etc, worked on QT, developt a lot of programs,  but when programming a game I find it difficult to move objects around the screen the way I want, or do behaviours like snake body movements, implementing a basic AI,..etc.

I had a problem with a basic algorithm here : http://www.gamedev.net/topic/647053-snake-body-movement-problem/

and I still didn't finish it...

 

1 ) What are the essential knowledge that is needed actually to program the objects, to do the behaviour ?

2) Do you guys write the behaviour, the debug and see, write, debug,..etc or write, test, run each time if you are programming the game objects, or writing the game AI, or the " Game Play " ?

3) I dozen of game programming books, like tricks of windows game programming gurus, LaMothe, Game Coding Complete,..etc. Nothing take about game play programming, how to move objects specific ways, how to really write the game.

Edited by AhmedCoeia

Share this post


Link to post
Share on other sites

If one problem your having is with movement not being smooth or uncontrollable, I think I may help.

Let's say you have a function that runs once every second through a tick system you've set up. If you want to move your object's x position by 5 pixels per second you'd do this.

StepFunc()
{
    Object.x += 5;
}

Now let's say that StepFunc is now called 30 times per second, but you want to keep moving at 5 pixels per second. You'd do this:

StepFunc()
{
    Object.x += ( 5 / 30 );
}

The thing is you can't always be sure that this function will be called exactly 30 times per second. It could fluctuate a little on regular use. In the case that the game slows down and now this function is taking longer than it should be called, the object is moving slower than it really should be. And in the case the game speeds up dramatically, the object will be moving faster than it should be and will also appear to be skipping along in both cases.

You can ail this hex by increasing your position based on the time elapsed from the previous movement. That way when this function is called more often than you intended, the object's x position movement is minimized. If the function is called less than what you wanted, the x position movement is upped to match the speed of 5 pixels per second. Your function would have to have an argument with the time elapsed since the last "tick"

StepFunc( double dTimeElapsed )
{
    Object.x += ( 5 * dTimeElapsed );
}

I explained it in the way I understood it. You can also refer here for more information on managing your time. http://www.koonsolo.com/news/dewitters-gameloop/

Share this post


Link to post
Share on other sites

Actually, the problem is with game play coding, like moving the players for an AI, making random movements like a flying saucer,..etc. I don't know the technqieus for achiving those things. Are there any books about that, or people just look at others code, or they run test infinetly, untill they get the effect ?

Edited by AhmedCoeia

Share this post


Link to post
Share on other sites

There are books out there, though I can't think of any particularly good ones I remember specifically.  But given the scope of your question you should really break things down a bit more.  In a generalized way of looking at things, there are three levels to what you are asking about.  There is the basic level of getting things to "move" properly when coded into a game loop (what Panda mentioned), then there is the basic control level which combines movements into simple concepts such as "turn and move to this point" and finally the higher level of "intelligence".

 

 

Most game books "learn game programming" books will cover the first two layers with a very very light version of the last layer.  Take an example pong game.  The ball is an example of all three layers in a very simplistic manner.  The first step is to make the ball move across the screen smoothly.  Now you add in the control layer which applies the rules of what to do when it hits the top or bottom wall (bounce) and hits or misses a paddle (bounce or score a point for the other player).  Thats actually a trivial but very good learning item to implement as a starting point.  The intelligence portion in such a game would be adding something to the second paddle so someone could play the game against the computer.  

 

While this sounds exceptionally trivial, learning something like this is the stepping stone to learning everything else you talk about.  Just learn each item in turn as appropriate.

Share this post


Link to post
Share on other sites

Generally, as with most things programming, it is just try stuff and see what looks the best or what plays the best. Gotta start somewhere though. When it comes to movement and gameplay, you should first have a decent idea of what it should look like in your head and then write pseudocode that you think will achieve the effect. Then you can look at your pseudocode, but the actual implementation will vary greatly depending on what language / engine / libraries you are using. If what you write is not giving the desired effect, then the next step is to answer WHY it is not giving the desired effect.

 

Remember that for smooth movement changes you need to be changing velocity using acceleration. If your movement is rigid, maybe you are incrementing velocity instead acceleration based on player input. Every frame should have:

position(new) = position(old) + velocity*dt + 1/2 * acceleration * dt^2

velocity(new) = velocity(old) + acceleration * dt

Share this post


Link to post
Share on other sites

It does start off difficult. It took me a week to make a monster constantly move up and down.

 

My advice would be to have a good understanding of how the graphics window work: how the coordinates are laid out like and how these coordinate behave: do they stay the same, increase or decrease? Like how the origin is on the top left of the window, x increase as you go right but y is constant. y increase as you go down but x is constant.

 

At the end of the day, they are just a bunch of logic more specifically if and else statements. A bunch of if and else statements.

 

Then take these ideas and write the ideas and the logic down a piece of paper. Just debug the code on paper and figure out the problems in the code. The bug can only be in one place and it will never move. Sometimes, it can also be logic that you forgot to write. I experience this when I wrote my first basic AI code.

 

The reason why programming books don't teach you these things is because programming is a general idea. There are infinite posssibilities on what you can do with it. You are not suppose to "plug and chug" and guess it is going to work. It needs to make sense and that can only be achieved through understanding, logic and being methodical the same why you would approach a math or physics problem with that kind of mindset.

 

It is like saying "why don't books teach you how to make final fantasy or starcraft or zelda? At the end of the day, the games share basic code structure. Besides, it will spoil the fun and also prevent the reader from experimenting and doing it themselves. You are suppose to have an open mind and use that mind to come up with the code yourself. That is the beauty of programming. It is suppose make you tap the power of your mind.

 

On top of my head, try getting the snake head to move. If you can make it move in all for four directions, try making the body movement along with the head. Take it one step at a time and build off of that.

 

There are many ways to solve it. This is just one way I came up with on top of my head.

 

Good luck.

Edited by warnexus

Share this post


Link to post
Share on other sites

the process is not that complex, but easier to explain with a concrete example.

 

can you select one thing as an example that you'd like to know how to do?

 

from that one example, you'll then apply the process repeatedly.

 

so pick an example, like your snake or something.

Share this post


Link to post
Share on other sites

ok right quick off the top of my head...

 

snake has 10 sections

 

each section has an x and y value.

 

so you'll want an array[10] of int x,y

 

section[0] is the head

 

to move the snake:

 

move each section, from tail to head, to the x,y of the section in front of it. then move the head to its new x,y

 

in c:

 
struct section
{
int x,y
};
 
section snake[10];
 
void move_snake(x,y)   // x,y is new location for head, body follows automatically. be sure to move the head just one tile at a time!
{
int i;
for (i=9; i>=1; i--) snake[i]=snake[i-1];
snake[0].x=x;
snake[0].y=y;
}
 
Edited by Norman Barrows

Share this post


Link to post
Share on other sites

The problem is that these games are assuming you are moving by clear obvious jumps every step. You have velocity and are moving smoothly around. Taking what I did last time, I think this should do what you need, but the movement might be slightly erratic since I haven't tested it.
 

#include <math.h>

double GetDistance( double x1, double y1, double x2, double y2 )
{
    double A, B, C;
    
    //A is the y length
    A = fabs( y1 - y2 );
    
    //B is the x length
    B = fabs( x1 - x2 );
    
    //A^2 + B^2 = C^2; C = sqrt( A^2 + B^2 ); C = distance between two points( yay right triangles ); Pythagoreans Theorem
    C = sqrt( ( A * A ) + ( B * B ) );
    
    return C;
}

void YourGameLoop()
{
    //Offset of segments in pixels
    double dOffset = 8;

    for ( int i = 1 ; i < nodes.size(); i++ )
    {
        if( i == 0 )
        {
            // Do calculations for new position
            
            //Set position and previous position
            nodes[i].m_PrevPos.x = nodes[i].m_Pos.x;
            nodes[i].m_PrevPos.y = nodes[i].m_Pos.y;
            nodes[i].m_Pos.x = newX;
            nodes[i].m_Pos.y = newX;
        }
        else
        {
            if( GetDistance( nodes[i-1].m_Pos.x, nodes[i-1].m_Pos.y, nodes[i].m_Pos.x, nodes[i].m_Pos.y ) >= dOffset  )
            {
                nodes[i].m_PrevPos.x = nodes[i-1].m_Pos.x;
                nodes[i].m_PrevPos.y = nodes[i-1].m_Pos.y;
                nodes[i].m_Pos.x = nodes[i-1].m_PrevPos.x;
                nodes[i].m_Pos.y = nodes[i-1].m_PrevPos.y;
            }
        }
    }  
}

I probably screwed up somewhere, but this should be a step in the right direction!

Share this post


Link to post
Share on other sites

The movement was jirky, and the loop starts with i =1, so I think that is a mistake ?

Furthermore, you're updating the previous nodes, when they reach a min-distance, is that correct ? but that will let the movement of the snake body's crawling..

 

I'm thinking about moving all the snake segments with their own velocity regardless head's position, and when the head's velocity changes, all of the other nodes change their velocity too ? 

Share this post


Link to post
Share on other sites

@J-locke,

 

The code fragment is the same that PandDragonThing wrote. I didn't change anything.

 

My code also just copies previous node postitions one by one. I still can not put an offset "WHILE" copying the previous node positions.  How that can be done actually ?

We are copying the previous node positions like :for (i=9; i>=1; i--) snake[i]=snake[i-1];  it can not be possible to the set offset here for sure ?

Share this post


Link to post
Share on other sites


We are copying the previous node positions like :for (i=9; i>=1; i--) snake[i]=snake[i-1];  it can not be possible to the set offset here for sure ?

 


The problem is that these games are assuming you are moving by clear obvious jumps every step. You have velocity and are moving smoothly around.

 

if you're not using tile based movement, all bets are off!

 

to do it with smooth movement, you'll need to do the cloth / rope modeling thing.

Share this post


Link to post
Share on other sites

why there is a need for tile based movement ?  

 

is it ok to just move the snake body movements and when the heads colloids, all other snake body segments change the velocity too ?

Some source code of Snake uses tile based or array-based implementation for collision. You can use a bounding rectangle on the head and use an interesect method of the rectangle class if the programming language supports it. You can do this in Java.

 

You mean if the snake eats an apple and update the speed of the snake? Sure you can do that. It is up to you. This idea is the rule of the game. It does make challenging gameplay.

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