PDA

View Full Version : How to move an object to the origin and back


conzar
11th September 2007, 11:08 PM
Hello. I would like to know how to move an object to the origin and then back to its original location. I've tried the code that is below; however, when the function exits, the object is left at the origin.


ACObject *obj = ... get from somewhere

// get the center of the object
Point3 *centre = ac_object_get_centre(obj);
// move it to the origin
translate_object_zero(obj);
// do something useful here

// move it back
translate_object(obj,centre);


I've also tried translate_object_abs and get the same results.

Anyone know what I'm doing wrong?

Using AC3D 6.2.05 linux version.

lisa
11th September 2007, 11:39 PM
What happens if you de-reference the pointer to the object center?

i.e.

Point3 ptCentre = *(ac_object_get_centre(obj));
translate_object_zero(obj);
translate_object(obj, &ptCentre);

conzar
12th September 2007, 09:04 PM
That worked! So why does it work?

Thanks for your help!

lisa
12th September 2007, 10:46 PM
When you get the pointer to the center, you're storing the address of the memory where the data is located, not the data itself. When AC3D moves the object, then center moves too, of course. If you're looking the center up through the pointer, you'll get the *new* location of the center because the data at that address has changed--which isn't what you want.

By de-referencing, you cause your code to store a *copy* of the center location, meaning the number won't change no matter how much you move the object.