0
votes

I am new to c++ as well as game development. I coded a function using bullet physics which returns the gravity of the world and it seems to be not working. It always returns 0. Though, I've initialized the world, solver, dispatcher, broadphase and collision configuration.

Here's the code:

void _setGravity(btScalar gravity) //sets the gravity of the world
{
    if(gravity > 0.f)
        gravity = -gravity;
    _dynamicsWorld->setGravity(btVector3(0, gravity, 0));
}

btScalar _getGravity(void) //returns the gravity of the world
{
return ((btScalar*)_dynamicsWorld->getGravity());
}

Is there something that I am doing wrong ?

Thank you.

1
That casting looks very suspicious, why are you casting the result to a pointer?Some programmer dude
I think the code of _dynamicsWorld setGravity and getGravity would help a lot. Also just a thought: You set the gravity as vector but return it as scalar....?Jiří Kantor
Instead of casting write a proper converter function. It will help you to understand where the problem is.CyberGuy
Don't use C-style casts. Use static_cast. If you need reinterpret_cast to make it compile, you probably have a bug.Neil Kirk
I am never sure of this (A*)a->x(); which is 1st -> or cast ?luk32

1 Answers

2
votes

The getGravity function returns a btVector3 by value, so you need to use resulting vectors getY to get the gravity:

return _dynamicsWorld->getGravity().getY();