Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cosine

Options
  • 02-04-2008 2:39pm
    #1
    Registered Users Posts: 83,306 ✭✭✭✭


    How do you implement the inverse of cosine (in C++)?


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    #include <math.h>
    double acos (double x);

    Or if you actually want to write your own function, erm... get a maths book! :D I've forgotten the details!


  • Registered Users Posts: 83,306 ✭✭✭✭Overheal


    thats just what i needed thanks :)

    (stupid cosine rule)

    edit: that would happen to be overload for use with float by any chace? :p


  • Registered Users Posts: 981 ✭✭✭fasty


    Yeah man, there's a float version

    float __cdecl acosf( float );

    Don't worry about the __cdecl bit.

    Just take a look in math.h and you'll see all it can do for you! :D Mine is in C:\Program Files\Microsoft Visual Studio 9.0\VC\include


  • Registered Users Posts: 83,306 ✭✭✭✭Overheal


    Awww.... reading? pfft. I have deadlines to meet and chicken to eat!

    thanks


  • Registered Users Posts: 981 ✭✭✭fasty


    I know, reading up on stuff is crazy talk. My coworkers laugh their heads off when I read the docs and look at source. Real men don't read the instructions apparently! :D


  • Advertisement
  • Registered Users Posts: 83,306 ✭✭✭✭Overheal


    Basically, my code is crashing with an exception saying I am trying to access memory location 0 while i am trying to add an object to a list.
    global:
    
    BrickController * pBrickCon; // object that controls a list of Bricks (for Breakout Game)
    
    main(){
    
    ...
    
    pBrickCon->Setup(...);
    
    ...
    
        while (myEngine->isRunning()){
           myEngine->drawScene();
           pBrickCon->Update();
           ...
        }
    }
    
    using tle::ISprite;
    
    class Brick : public ISpriteWrapper 
    {
    public:
        Brick(string const & imageName, int x, int y, int id);
        bool Update(Ball *pBall);
    };
    
    class BrickController
    {
    private:
        int xCols, yRows, spacing;
        SLinkedList<Brick*, SListIterator<Brick*>> brickList;
    public:
        
        BrickController();
        void Setup(int SCREEN_WIDTH, int SCREEN_HEIGHT, int spacing); //setup list structure of bricks
        bool Update(Ball * pBall); //update bricks.
        ~BrickController(){}
    };
    
    Brick::Brick(string const & imageName, int x, int y, int id) :
    ISpriteWrapper(imageName, x, y, id) {}
    
    BrickController::BrickController() {}
    
    void BrickController::Setup(int SCREEN_WIDTH, int SCREEN_HEIGHT, int spacing)
    {
        ImageResource::s_spriteType = BRICK;
        brickList.insertFirst(new Brick(ImageResource::BRICK_NAME,0,0,ImageResource::s_spriteType));
        SListIterator<Brick*> iter = brickList.begin();
        int width = (*iter)->d_width + spacing; // x-distance between 2 bricks
        int height = (*iter)->d_height + spacing; // y-distance between 2 bricks
        Vector spawn( width + spacing, 0); // A marker used to determine where the next brick is placed.
    
        for( ; (spawn.x < (SCREEN_WIDTH - width )); spawn.x + width ){
            for( ; (spawn.y < ((SCREEN_HEIGHT / 2) - height + spacing)); spawn.y + height ){
                brickList.insertLast(new Brick(ImageResource::BRICK_NAME,spawn.x,spawn.y,ImageResource::s_spriteType));
            }
        }
    }
    

    the crash is occuring at the insertFirst.


  • Registered Users Posts: 981 ✭✭✭fasty


    Make sure all the parameters you're passing to the Brick constructor are valid. nesting the new Brick line in insertFirst makes things look tidier, but harder to debug. Is it really insertFirst that's causing the error?
    Brick* testBrick = NULL;
    testBrick = new Brick(ImageResource::BRICK_NAME,0,0,ImageResource::s_spriteType);
    if(testBrick != NULL) brickList.insertFirst(testBrick)
    else // do some error handling here
    

    At a guess, I'd have the problem is with ImageResource::BRICK_NAME or more than likely ImageResource::s_spriteType. What type of string is it?


  • Registered Users Posts: 83,306 ✭✭✭✭Overheal


    Nope I found the error when I tested my Update function by hard-writing it into main: I had never initalised a new BrickController object into the brickCon pointer. Whoopsie!
    main(){
    
    ..
    
    pBrickCon = new BrickController(); // is what i needed
    
    pBrickCon->Update(...); // somehow this managed to call anyway, and it crashed when it tried to load the class definition of the list.. or something like that.
    


  • Registered Users Posts: 981 ✭✭✭fasty


    Good old null pointers!

    Get into the habit of initialising pointers to zero and before you dereference them, make sure it's value is != 0;
    MyClass* pMyClass = 0;
    
    // some code, including we assume pMyClass = new MyClass() buy maybe you forgot!
    
    if(pMyClass != 0) pMyClass->SomeFunction();
    else // error
    


Advertisement