limiting an objects movement

Software: Away3D 4.x

ThaFresh, Newbie
Posted: 15 November 2013 07:14 AM   Total Posts: 2

I have an object that can be moved by the user, I would like to restrict its movement to be within a radius of the starting pos

checking its distance to the startpos is easy with

var moveLimit:number = 100
var dist:Vector3D = playerBody.position.subtract(startPos);   
if (dist.length > moveLimit) {
    //do something to prevent movement
}

I assume I next need to calculate a new position vector thats 100 units along from startpos towards the playerbody position. Anyone know how to do that?

   

Vishwas Gagrani, Newbie
Posted: 25 November 2013 06:15 AM   Total Posts: 2   [ # 1 ]

If you apply simple math, it would be within the sphere of 100 unit radius. So, you upon each bit of movement, you will need to calculate if your position vector x,y,z comes within 100 units.

So, for the new position vector all the following conditions must be met:

1)  sqrt ( x^2 + y^2 )  <= 100
2)  sqrt ( x^2 + z^2 )  <= 100
3)  sqrt ( y^2 + z^2 )  <= 100

That should work

Vishwas

   

Avatar
Mu Duck, Newbie
Posted: 25 November 2013 02:35 PM   Total Posts: 20   [ # 2 ]

You want to move it in the sphere or just a circle? I suppose the latter in the XZ plane. So you can go with something like this:

var vec:Vector3D //your starting position
var mesh:Mesh //your object
var sx:Number, sz:Number //movement increments for 2 directions

if(Math.pow((mesh.x+sx-vec.x), 2) + Math.pow((mesh.z+sz-vec.z), 2) <= 10000) {//means it is inside the circle
  mesh.x +=sx; mesh.z +=sz;
}

//it is better not to take sqrt, because it is an additional unnecessary action (just always keep optimization in mind)

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X