setUserIndex alternative?

Post Reply
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

setUserIndex alternative?

Post by Crayder »

Okay, first off let me point out that I'm already using setUserIndex for something important. Now I need save something else just as important. There is setUserPointer but it is a void. I need another way to save integers.

So for each rigid body I'm currently saving a single integer using setUserIndex, but I need to save more integers. How do you recommend I go about this?


(edit: in-case you need to see the project, https://github.com/Pottus/ColAndreas)
d3x0r
Posts: 51
Joined: Tue Dec 11, 2012 9:59 pm

Re: setUserIndex alternative?

Post by d3x0r »

make a struct that contains the integers's you'd like to save and store that instead? (or actually user pointer instead... or keep an array of those structs and use the int as an index into that struct?)

setUserPointer works exactly the same as index... so I assume you mean like setUserIndex would be 0 if unused...
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

Re: setUserIndex alternative?

Post by Crayder »

d3x0r wrote:setUserPointer works exactly the same as index... so I assume you mean like setUserIndex would be 0 if unused...
No it doesn't.

setUserIndex stores integers.
setUserPointer stores void pointers.

setUserIndex is only capable of storing one integer per rigid body. I need to store more than one integer.
d3x0r
Posts: 51
Joined: Tue Dec 11, 2012 9:59 pm

Re: setUserIndex alternative?

Post by d3x0r »

Crayder wrote:
d3x0r wrote:setUserPointer works exactly the same as index... so I assume you mean like setUserIndex would be 0 if unused...
No it doesn't.

setUserIndex stores integers.
setUserPointer stores void pointers.

setUserIndex is only capable of storing one integer per rigid body. I need to store more than one integer.
struct s {
int one_integer;
int more_than_one_integer;
}

s *info = new( s );
setUserPointer( s );
d3x0r
Posts: 51
Joined: Tue Dec 11, 2012 9:59 pm

Re: setUserIndex alternative?

Post by d3x0r »

I mean that it work exactly the same in that if you don't setUserIndex to anything getUserIndex will be 0... same if you don't setUserPointer, getUserPointer will of course be null.
Crayder
Posts: 15
Joined: Mon Aug 10, 2015 10:18 pm

Re: setUserIndex alternative?

Post by Crayder »

d3x0r wrote:I mean that it work exactly the same in that if you don't setUserIndex to anything getUserIndex will be 0... same if you don't setUserPointer, getUserPointer will of course be null.
I'm sorry but I'm not very pro on C++. Can you go a little more in depth on how to do it with examples? Perhaps with an example on how to apply it and get it from a btRigidBody?
d3x0r
Posts: 51
Joined: Tue Dec 11, 2012 9:59 pm

Re: setUserIndex alternative?

Post by d3x0r »

Crayder wrote:
d3x0r wrote:I mean that it work exactly the same in that if you don't setUserIndex to anything getUserIndex will be 0... same if you don't setUserPointer, getUserPointer will of course be null.
I'm sorry but I'm not very pro on C++. Can you go a little more in depth on how to do it with examples? Perhaps with an example on how to apply it and get it from a btRigidBody?
1) there's no difference in C++ between a struct and a class except the default protection... struct is public: and class is private: by default. structs can have methods etc just like a class.

the same way you set and get the index? but subst Index for Pointer and an address of a structure containing multiple values instead of a single value?

Code: Select all

// define a struct/class somewhere
struct your_values {
   int a, b, c;
   double e, f, g;
   your_values( int _a, int _b ) { a = _a; b = _b; }
};
-----------

Code: Select all

btRigitBody* rb; // assume you can initialize this so it's not NULL or invalid.

void f() {
   your_values *vals = new your_values( 1, 3 );
   rb->setUserPointer( vals );
}
.....

Code: Select all

// later get the value back
your_values *vals = (your_values*)rb->getUserPointer();
//though I guess the current standard looks more like
your_vals *vals = static_cast<your_values*>rb->getUserPointer();

which looks something like

Code: Select all


rb->setUserIndex( 1 );
//and later
int val = rb->getUserIndex();

int stays 32 bit when moving to 64 bit ... so an int value itself is bad to store a pointer in. But one COULD do something like...

Code: Select all


FILE *file = fopen( "some file for fun.txt", "wb" );
rb->setUserIndex( (int)file ) );

// and then later ....
FILE *file = (FILE*)rb->getUserIndex();  

but again, this is a bad thing to do, and one should just use the appropriate pointer type instead. using void* type allows you to pass any pointer without casting it... like in the last example to store the FILE* as an int required casting it with an (int).

But either way you have to cast the result out.

I'm no good in C++ either; other than I know the basics of inheritance, protections, overloaded functions etc... which is why I don't really know why one would use static_cast<whatever> instead of just (whatever*).

-------
if this didn't give you an answer you'll have to ask more specific questions.
Post Reply