Copy object, translate and then play animation

11 July 2017 22:59
I am trying to build a scene were objects are created dynamically by copying other objects in the scene, repositioned (translation), and then have an animation action applied to them in that new location. I cannot get this to work.

When I want to animate an object that will be translated, I assign it to a parent. This parent is usually an empty. I can then translate the parent and apply the animation action to the child. This technique works well.

However, I now want to create new objects with the m_obj.copy() API and animate them. I have tried this without copying the parent and it did not work. Once the animation action is applied to the copied object, the action is preformed relative to the scene origin rather than the location where the copied object was place into the scene. This makes sense, because animation actions seem to be preformed relative to their parent's coordinate system, which, in this case, is the scene.

So I tried to copy both the parent and the child, create a constraint between them, translate the parent, and then apply the animation action. This does not work. The new parent and child objects do appear in the scene, but the animation will not play. The new child remains fixed to the new parent.

Here is the code in my callback

function move_cube_cb(in_params, out_params) {
    //console.log("In move_cube_cb");
	var src_obj_child = m_scs.get_object_by_name("ChildGreenCube");
	var src_obj_parent = m_scs.get_object_by_name("ParentRedCube");
	var cube_copy_child = m_obj.copy(src_obj_child, "cube_copy_child", true);
	var cube_copy_parent = m_obj.copy(src_obj_parent, "cube_copy_parent", true);
	m_const.append_semi_stiff(cube_copy_child, cube_copy_parent, m_vec3.create());
	m_trans.set_translation(cube_copy_parent, -5, 0, 4);
	m_anim.apply(cube_copy_child, "MoveAway", 0);, anim_callback, 0);

Is there a better way do doing this? Is there something wrong with this code?

Any help that you can provide would be appreciated.
You have an awesome product!
12 July 2017 00:13
append_stiff_trans_rot() might work for you. You can use this to attach your object to whatever empty you are animating.
remove() can then be used to detach it.
12 July 2017 04:22
Hi Will,
I tried your suggestion. It did not change the behavior of the new copied child object. The copied child object will not animate once it is attached to another object with either one of these constraints.

But… there is something in what you said. You described animating the empty (the parent) to move the child. That is not what I was trying to do. I was applying an animation to the child object, not the empty. This usually works in B4W, but not in this scenario where the objects are copies and not straight from from the blend file.

I liked this approach because the empty could be easily repositioned and the empty would act as a LCS for the child's animation.

Maybe this is impossible. Maybe I am doing it wrong. I am certain that there is a better technique.

I will tinker with animating the empties rather than the children, as you said.

12 July 2017 09:17
You might take a look at the Cartoon Interior project in your SDK. There is a long thread about it here.
This project loads objects then moves them around by mouse dragging.
Please register or log in to leave a reply.