Lookat not working as expected

Software: Away3D 4.x

m_pedro, Newbie
Posted: 05 February 2013 05:54 PM   Total Posts: 7

The idea is to have an object “follow” another object and stay behind it slightly. I am trying to figure out the best way to handle this and have tried many different things over the past couple of days but I can’t get it to work correctly.
The ideal solution would be to group the objects in a container, offset the object, and have the object simply lookAt the other. However, I have physics applied to one of the objects so I don’t think that adding it to the container would work. Right now I have the object with physics separate and a container with the other object that is offset by 10. This works to place the object behind the other but when the lookat is applied, it doesnt seem to look directly at the main object.

var ball:Mesh = new Mesh(new SpereGeometry());
 var 
cube:Mesh = new Mesh(new CubeGeometry());
 
cube.moveBackward(10);

 var 
container:ObjectContainer3D = new ObjectContainer3D;
 
scene.addChild(container);

 
container.addChild(cube); 

now in enterFrame…

container.position ball.position;
cube.lookAt(ball.position); 

This code isn’t taken from my program but is a representation of what it is doing.

I would like the cube to lookat the ball regardless of its position. It almost works but it seems the offset it messing with the rotation. The ball needs to move around the screen, being chased by the cube.
Any help is appreciated, thank you for your time.

   

m_pedro, Newbie
Posted: 07 February 2013 12:54 AM   Total Posts: 7   [ # 1 ]

Anyone have an ideas on how to fix this or another solution? I may be going about this all wrong. I have continued to mess with this and still can’t figure it out. Is there possibly a way to offset the cubes position without adding it to an objectContainer because that would solve the problem as well. I need it to always be following the sphere so an update has to occur in the enterFrame event.

Thank you

   

Avatar
alihm, Jr. Member
Posted: 07 February 2013 03:42 PM   Total Posts: 49   [ # 2 ]

Here is my solution:

distanceV:Vector3D = cube.position.subtract(ball.position);
distanceV.normalize();
distanceV.scaleBy(1000);
distanceV= distanceV.add(ball.position);
cube.position = distanceV.clone();
cube.lookAt(ball.position);

So we basically create a vector between two objects and set it’s length to 1000. then use it to offset the cube from ball.

Hope it helps,

   

Avatar
Fabrice Closier, Administrator
Posted: 07 February 2013 04:43 PM   Total Posts: 1265   [ # 3 ]

I don’t know if fully applicable, but you might want to play a bit with the follow behaviour in Prefab. Where you can set a range, increase and distance. For instance the speeding car passes police car nearby, the police car chase then the speeding car, and then stop (not in range anymore) because they do not have enough speed to catch it. The distance is there to prevent car police to enter bounds of speeding car and the increase the update.
You can test this realtime in a sec in the app. If you want take a look at package or use it. You can download from the as3 panel. To see/get the code implementation export with project option on.

   

m_pedro, Newbie
Posted: 07 February 2013 09:06 PM   Total Posts: 7   [ # 4 ]

Thank you very much for the replies

Alihm, this seems to work pretty well except when the sphere changes certain directions, the cube will rotate over top of it to get to the other side.
I will try and work with what you gave me though. Perhaps ground clamping the cube will work. Thank you

Fabrice, while this doesn’t work for what I am trying to do at the moment, it is awesome. I didn’t notice this in prefab before but I just tried it and added it to my project and I can definitely see a use for this in the near future. I think how I wrote that I needed a “follow” i was being sort of misleading, what I really am trying to do is more of an “attach”. The objects are to be positioned a precise distant from each other at all times with the cube looking at the sphere. A better example would be a person pushing a wheelbarrow, the person is always attached and if the wheelbarrow rotates, the person rotates with them.

Thanks again

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X