UserData

From Newton Wiki
Jump to: navigation, search

Purpose

User data in Newton provides a way for you to store your own information in a Newton structure. This is especially necessary with static callback functions, so you can identify individual bodies, worlds, joints.. without needing a unique callback for each one.

Data type

Conveniently, on 32 bit systems this will always be the same size as a pointer. You can simply store a pointer to your graphical object inside a body, for example, so you can just pull it back out again in a TransformCallback, cast it back to your graphics object, then update its position to keep it in sync with the body.

Example usage

Imagine a chair class. That has a static NewtonSetTransform callback, and a pointer to a NewtonBody representing it. When the chair's body has moved, the NewtonSetTransform callback is called. How do we know which chair has moved? Well you can just get the user data associated with the body, and either cast that to a chair (if you stored a pointer) or look it up in a map (if you stored an id), then modify the chair's position.

Another use would be to simply store an ID, and use that to look up your objects yourself.