Is anyone willing to give me help on a game im making

Ok so i have multiple problems that are quite large.
None are like compiler problems.
I have quite a lot of code as well.
And all of it is relevant so im not quite sure how to go about asking for help with it.
Anyway my problems are.

1) I have a state thing like on the lazyfoo tutorial but i dont want to use global variable so im unsure of how to change the state when needed. I had ways of accomplishing this but they seemed wrong like passing the pointer to the currentstate to the state it was pointing to then returning a new state if needed.

2) Creating a level with height and width and then giving objects or graphics specific positions. Like the camera would focus on a single object but then if an object went into the cameras view it would be seen.
I was thinking of creating an array of all the objects then checking if they entered the view of the camera but again this seemed wrong.

3)Pretty much the same as 2 but performing collision detection based on the position of an object in the level rather than its position on screen.

I would be very grateful if someone could provide me with an answer to the best way of accomplishing even one of these problems :)

Also if my code is needed ill post or someone can tell me a site to put it on or something or whatever :)
closed account (N36fSL3A)
I can give you source code to an entire project using sdl if you'd like.
yeah i could have a look but i dont know if it would solve my problems.
Do you have any answers to the problems ?
Thanks though:)
It will probably help on other things :)
1.) The nature of a Game State is very much like the behavior of a Stack.
Essentially, you want to be able to push and pop states from a stack of Game States. For example: Pushing a 'Paused' Game State on top of an 'In-Game' Game State.
Unfortunately, I don't have any code to supply you with. Read up on stacks if you aren't already familiar with them.

2.) How are you rendering? Are you using SDL, SFML, DirectX or OpenGL? Possibly a combination of those? If you're using OpenGL, I can tell you that there is no such thing as a 'Camera'. To simulate camera-like behavior, one could make a Camera class which wraps OpenGL's native matrix transformation methods.
As for the second part of that question, what you're describing is known as "Culling". Depending on the kind of viewport you're using, "Frustum Culling" or
"Viewport Culling" would be terms to Google. Basically, culling is used to determine which areas or entities lie outside of the viewport (the visible area). This information can then be used to ignore all the flagged entities when you are rendering. It's advisable to store the entities in a dynamic container (std::vector<>, etc) and then to iterate through them.

3.) You could use a tree-structure for this. Off the top of my head I'm thinking Quadtree's, though I've only been able to apply them effectively in a 2D environment. You can use tree-structures to flag entites for collision checking as well as do frustum culling with them, pretty neat.

Depending on the kind of viewport you're using, "Frustum Culling" or
"Viewport Culling" would be terms to Google. Basically, culling is used to determine which areas or entities lie outside of the viewport (the visible area).


You are thinking of "clipping"

"culling" is the process to determine which side of the polygon is the "front" and which is the back.
Last edited on
@Disch Woops you're right :)
I think im already familiar with how the stack works although im not sure what you mean by a stack of gamestates?
How would you set that up?

Also im already familiar with clipping(again i think) as i use it for my power bar.
Im not sure how to implement this.

Say i had a level 2000 x 1000
and my screen size is 800 x 600 px

And i wanted something to appear at position 1000, 600.

How would i do this ?
Would you be able to provide any super basic code just so i can get the jist :)

Also im going to look into quad trees right now :)
If your screen is 800 x 600 you may have to zoom out or something I am guessing also I would maybe suggest investing $10 in a new monitor :P jokes.
That was an example.
Also i meant the size of the program window.
also i forgot to thank xismn for the info.
so thanks :)
Last edited on
Games typically have 2 coordinate systems: "world" coordinates and "camera" coordinates. World coordinates represent a point in the game world, whereas camera coordinates represent the world relative to how it is displayed on screen.

The game should typically operate in "world" coordinates for just about everything. The only time you'd usually deal with camera coordinates is when you draw to the screen.

With more modern graphic APIs, this is very simple, as there's usually some kind of interface you can use to automate the conversion.

With DirectX/OpenGL, you can set up a translation matrix to move the "camera" around... which allows you to draw everything using world coordinates... and the matrix math will automatically convert those to camera coordinates upon rendering.

SFML has something similar in the 'View' class, where you specify a camera position and apply the View to the display window and it will automatically convert world coords to camera coords upon rendering.

SDL 1.x ... I'm not so sure has anything similar, so you might have to do the conversion yourself (if that's what you're using). It's simple to do, it's just inconvenient. Instead of drawing an object at coords x, y (in world coords), you'd draw it at x - cameraX, y - cameraY, where cameraX/Y is the current position of the camera (ie, positive X values would scroll the world to the left, exposing more of the right).



I can't get any more specific than that without knowing what graphic API you're using.
Last edited on
Thanks for the infoagain.
im using SDL btw.
Im still not sure exactly how to implement it though.
Would it be possible to show a (simple) practical example.
Say i had 3 objects and their respective positions in a level of size 2000* 1000 with a window size of 800 * 600
Ob1 200 * 300
Ob2 800 * 600
Obj3 1600 * 600
Id really appreciate it :)
closed account (N36fSL3A)
I've actually solved that problem. (Actually, LazyFoo did.)

This is how I did it.

I have an SDL_Rect called camera.

Set the width and height to window coordinates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int LevelW = 2000
int LevelH = 2000

SDL_Rect Camera = {0, 0, 800, 600};

int CameraX;
int CameraY;

// Lets say you have a tile map. Initialize those tiles the class should look something like this

class Tile
{
    public:
        Tile(int x, int y)
        {
            // Initialize it and whatever
        } 
        bool ChkCollidied(SDL_Rect Entity)
        {
            // Use your collision detection function here
        }

        void Render()
        {
            Draw(SDL_Surface* Screen, Clips[Type], X - Camera.x, Y - Camera.y);
        }
    private:
    int X;
    int Y;

    int Type;
    
    SDL_Rect Clips[10]; // Lets say I have a tileset with 10 tiles
    
    SDL_Rect Box; // Collision box
};


Matter of fact, it's best you just PM me if you want the code to my project.
Last edited on by Fredbill30
Matter of fact, it's best you just PM me if you want the code to my project.


Is that because you'd like to avoid questions like "Why would each instance of a class Tile maintain a set of tiles?"

closed account (N36fSL3A)
It doesn't?
Topic archived. No new replies allowed.