Platform game gravity problem

This is a discussion on Platform game gravity problem within the Game Programming forums, part of the General Programming Boards category; What you do: Code: charater* p1; p->whatever(); What you must do: Code: // Either character* p1 = new character; p1->whatever(); ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    What you do:
    Code:
    charater* p1;
    p->whatever();
    What you must do:
    Code:
    // Either
    character* p1 = new character;
    p1->whatever();
    delete p1:
    
    // Or
    character p1;
    character* pp1 = &p1;
    pp1->whatever();
    This boils down to how pointers work.
    The first example creates no character whatsoever and tries to access a non-existent character.
    The second example shows two ways of creating a character and setting the pointer to point to it.

    As for the class, I'll demonstrate a function:
    Code:
    void foo(character* p)
    {
        p->whatever();
    }
    
    int main()
    {
        character p1;
        foo(&p1);
    }
    References are also possible:
    Code:
    void foo(character& p)
    {
        p.whatever();
    }
    
    int main()
    {
        character p1;
        foo(p1);
    }
    Foo needs to take the player it needs to work on as an argument.
    Remember: classes are ONLY blueprints. Like a car blueprint.
    A car does not exist until it's built from a blueprint. And there can be several cars from the same blueprint.
    The same applies to characters.

    This is the basic understanding I want you to understand, which is why I said go back to studying pointers.
    Last edited by Elysia; 02-21-2009 at 11:12 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Thanks! You're awsome!
    I belive that this might be the last thing I needed to learn about pointers, but that's prolly mostly 'cause I wanna belive that xP
    the function stuff, I think I knew
    Thanks again!
    Currently research OpenGL

  3. #18
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Damn, hope I ain't bothering you peeps too much :S
    but, the program runs fine, but it doesn't do what I want.. I want all classes inherited from character to be affected by gravity, but they don't, do I really have to make a code for every class, or can this be done automaticly? like, do I have to call the function once for each class under character, or is there a way to call all, using one function?
    my gravity() is virtual, I thought that was supposed to do it, but I guess I'm missing something? :S
    Thanks in advance!
    Currently research OpenGL

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    That's because you have the wrong function virtual.
    The concept is that when you call a function via a base class pointer or reference, it looks up the type, and calls the function in the actual type stored inside the pointer or reference. But you don't need that with the world manager.

    But you've already laid the base. If each class sets the gravityPull (even though gravity actually pulls all objects towards it at the same speed) member variable of the character class upon construction, the current function will work for all types of classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Code:
    class worldManager {
    private:
    	character* chr;
    
    public:
    	worldManager();
    	~worldManager();
    
    	void gravity();
    
    };
    Code:
    void worldManager::gravity(){
    	chr->pullDown();
    	
    }
    Code:
    class character {
    	
    protected:
    	void renderQuad(HGE* hge, hgeQuad* quad);
    	void flipLeft(hgeQuad* quad);
    	void flipRight(hgeQuad* quad);
    	void updateQVert(hgeQuad* quad);
    
    public:
    	character();
    	virtual ~character();
    
    	virtual void pullDown();
    
    	float x, y;
    	float z;
    	float gravityPull;
    	hgeRect charRect;
    	
    };
    Code:
    void character::pullDown(){
    	y += gravityPull;
    }
    Code:
    player::player(){
    
    	x = 100.0f;
    	y = 100.0f;
    	z = 0.5f;
    	speed = 250.0f;
    	gravityPull = 0.5f;
    }
    Dun keel me :S
    But it won't work >.< please, I wanna get this working before I have to go to this party :S
    above is all the code you should need, I'm not understanding it all fully... It all looks like it should be draggin the player downwards to me...
    Please help, and thanks in advance!
    Currently research OpenGL

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Code:
    class worldManager {
    private:
    	character* chr;
    
    public:
    	worldManager();
    	~worldManager();
    
    	void gravity();
    
    };
    Told you a dozen times already.
    chr points to neverland!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    woops forgot one part xP

    Code:
    worldManager::worldManager(){
    	chr = new character;
    }
    
    worldManager::~worldManager(){
    	delete chr;
    }
    I do know how to listen
    Currently research OpenGL

  8. #23
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    woops forgot one part xP

    Code:
    worldManager::worldManager(){
    	chr = new character;
    }
    
    worldManager::~worldManager(){
    	delete chr;
    }
    I do know how to listen
    Currently research OpenGL

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Alright, so now everytime worldManager::gravity is called, character worldManager (as I would like to call him) is pulled down. Every time. Wonder why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    huh o.O? it's supposed to make all the ones under character, being pulled down all the time, atleast for now, like gravity, you're constantly pulled down.
    Currently research OpenGL

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You are confusing classes and instances!
    Character is a CLASS, a BLUEPRINT, not an instance!
    Each PLAYER and MONSTER and ITEM are instances.
    And function are supposed to modify the STATE and INTERNAL DATA of instances! Of actual players, items and monsters!
    But you are only creating ONE and ONE single character and keep calling its member functions. What's the point?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, the point was making all classes under character, being affected by the pullDown(), just by calling it through character, and I asked if this was possible, and I understood by you that is was...
    Currently research OpenGL

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Read again: the compiler looks up the ACTUAL type of the object pointed to by the pointer or reference from where you call the function and calls the appropriate function in the appropriate class.
    But you are creating a SINGLE character of type... character.

    Base code:
    Code:
    class Creature { virtual void foo(); }
    class Monster: public Creature { virtual void foo(); }
    class Character: public Creature { virtual void foo(); }
    This is not polymorphism:
    Code:
    void foo()
    {
        Creature* useless = new Creature;
        useless->foo();
        delete useless;
    }
    
    int main()
    {
        Monster m1, m2, m3;
        Player p1, p2, p3;
    
        foo(); // Calls useless::foo
        foo(); // Calls useless::foo
        foo(); // Calls useless::foo
        foo(); // Calls useless::foo
        foo(); // Calls useless::foo
        foo(); // Calls useless::foo
    }
    This is polymorphism:
    Code:
    void foo(Creature& p)
    {
        p.foo();
    }
    
    int main()
    {
        Monster m1, m2, m3;
        Player p1, p2, p3;
    
        foo(p1); // Calls p1::foo
        foo(p2); // Calls p2::foo
        foo(p3); // Calls p3::foo
        foo(m1); // Calls m1::foo
        foo(m2); // Calls m2::foo
        foo(m3); // Calls m3::foo
    }
    Make sure you understand the difference!!
    The first example is what YOU are doing right now.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Just a sidenote, I see that many use foo(), is this some pre-made function?
    And I might just be getting it now
    Thanks again!
    Currently research OpenGL

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 03:29 PM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21