Thread: Platform game gravity problem

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Platform game gravity problem

    I have this class called player which I want to set gravity to, making it get pulled down until it hits a platform. Now i ahve this code for doing this
    Code:
    	if(P1.charRect.Intersect(&plat1->rect)){
    
    	}
    	else P1.y += P1.gravityPull;
    And P1 is called like: player P1;
    But I wanna avoid having to have such a code for each player, how do I do this?
    Is there a way I can implement this gravity code into the class? So that each of the players will be gravity affected when the class is called?
    Currently research OpenGL

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually, there would be a gravity class or world manager or something that takes care of that, I think, since it's not part of being a player.
    But I would think you need to hold your horses -- you still haven't learned pointers properly, nor polymorphism, which are ideal for big projects.
    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.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I'm not giving up xP a part of this project, is to learn while I code it
    And I'll try making a new class for it, thanks
    Currently research OpenGL

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But the idea is not to give up, but perhaps learn polymorphism first and see how it can radically change your project and give you incredible power as you design it.
    Because this is a very good example of where polymorphism is good. Your game manager will apply gravity to objects, monsters and players alike. Perhaps even more stuff.
    So are you going to create functions for each one of these types that it can accept and use? Wouldn't it be more appropriate that it has one function and it works on all the different types of things without losing type information or using some sort of casts with type information? That's what polymorphism is all about.
    Last edited by Elysia; 02-21-2009 at 08:30 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.

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I'm having some problems understanding polymorphism and with my code xP
    I added

    Code:
    virtual void pullDown();
    to my character class, and then I wanted to make a gravity() function in the class 'worldManager', but when I use character* chr; I ain't allowed access to pullDown()
    And I found no way of making the pullDown() function in worldManager, without hitting problems, I forgot why I had problems with that tho.. Will try again xP
    Currently research OpenGL

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The pullDown rightly belongs in the worldManager class. Usually the character class should expose enough functions for the worldManager to do its work. Hopefully. Otherwise you might need friends.
    And remember that all functions called needs to be public, or it cannot access them.
    Also remember that you can use references.
    Now without seeing code, I can only guess that you aren't using public functions, so I cannot say for sure.
    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. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    worldManager.h
    Code:
    #ifndef WORLDMANAGER_H
    #define WORLDMANAGER_H
    
    //worldmanager.h
    #include "character.h"
    
    class worldManager {
    private:
    	character* chr;
    
    public:
    	worldManager();
    	~worldManager();
    
    	virtual void gravity();
    
    };
    
    
    #endif
    worldManager.cpp

    Code:
    #include "worldManager.h"
    
    worldManager::worldManager(){
    }
    
    worldManager::~worldManager(){
    }
    
    void worldManager::gravity(){
    	chr->y += chr->gravityPull;
    }
    Manager call

    Code:
    mngr->gravity();
    The code compiles, but it crashes. And when i'm in debug mode I see that everytime I use pointers to my classes it says access violation.
    Last edited by Akkernight; 02-21-2009 at 11:13 AM.
    Currently research OpenGL

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does chr point to a valid instance?
    You need to focus on what you're doing. How does the classes interact? What does it mean to store a single pointer to one character inside the world manager? What benefit does it have? And what drawbacks does it have?
    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.

  9. #9
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Well, I point to character, so I can work directly with character without making a new instance of it, right? o.O
    And how can i make classes interact? xP
    Currently research OpenGL

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dude, a pointer must point to a valid instance.
    Classes interacting basically means they get, set and exchange information with each other, such as the world manager class handling the gravity for the player.
    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.

  11. #11
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    yeah, the pointer points to the character class o.O
    And how do I make them interact? Ain't it by using a pointer?
    Currently research OpenGL

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are only specifying that it it can hold an address of an object of type character. It does NOT point to a valid instance of an object. You cannot modify that which does not exist!
    Go back and read up on pointers before using them. This is catastrophe.
    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.

  13. #13
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    o.O How do I then call character so that all other classes that are inherited from it get affected by what I do then? If I understood it right, this is what virtual does, but how am I supposed to call character class to get access?
    EDIT: And I know I can be frusterating, but I just don't get it, I really appretiate your help tho
    Currently research OpenGL

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem does not lie in your polymorphism, but by how you handle your object instances. By how you handle your pointers wrongly!!
    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.

  15. #15
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    then tell me how I'm supposed to do it? :S
    give a small example, please!
    I can't learn anything if I'm just told I'm wrong, but not how I'm wrong and I'm not clever enough to understand what you're shouting about :S
    So please tell me how to handle them right?
    Currently research OpenGL

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, 04: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, 05:00 PM