Random Motion Path Code?

Random Motion Path Code?

Anonymous
Not applicable
526 Views
3 Replies
Message 1 of 4

Random Motion Path Code?

Anonymous
Not applicable
I need a way of making an object pick a random point on a grid like say (X = rand(-25,25) Y = rand(-25,25)) then move towards that point. If it reaches that point then it picks a new point then repeats. I need this to run in the expression . Please Help!
0 Likes
527 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
//- random position expression by Pavel Pehlivanov -
//- ------------------2011-02-03--------------------
//---- to setup scene for this add custom attrs ----
//------ "randX" and "randY" to target object ------
//--------------------------------------------------

$myObj = "pCube1"; //target object name
$xMin = -10; //x min var
$xMax = 10; //x max var
$yMin = -10; //y min var
$yMax = 10; //y max var
$threshold = .05; //if object reaches distance below this var it creates new position
$spd = .1; //speed multiplier

//get object pos
float $crX = getAttr ($myObj+".tx");
float $rnX = getAttr ($myObj+".randX");
float $crY = getAttr ($myObj+".ty");
float $rnY = getAttr ($myObj+".randY");

//calculate threshold
$tX=abs($crX-$rnX);
$tY=abs($crY-$rnY);

if(($tX<$threshold)&&($tY<$threshold)){
//if wanted position is reached create new random pos
setAttr ($myObj+".randX") (rand($xMin,$xMax));
setAttr ($myObj+".randY") (rand($yMin,$yMax));
}else{
//else change object position
setAttr ($myObj+".tx") ($crX+(($rnX-$crX)*$spd));
setAttr ($myObj+".ty") ($crY+(($rnY-$crY)*$spd));
}


Ask if you have any questions.
0 Likes
Message 3 of 4

Anonymous
Not applicable
Another implementation using velocity vector:

//- random position expression by Pavel Pehlivanov -
//- ------------------2011-02-03--------------------
//---- to setup scene for this add custom attrs ----
//---- "randX" "randY" "randZ" to target object ----
//--------------------------------------------------

$myObj = "pCube1"; //target object name
$xMin = -10; //x min var
$xMax = 10; //x max var
$yMin = -10; //y min var
$yMax = 10; //y max var
$zMin = -10; //z min var
$zMax = 10; //z max var
$spd = .5; //speed

//get object pos
float $crX = getAttr ($myObj+".tx");
float $rnX = getAttr ($myObj+".randX");
float $crY = getAttr ($myObj+".ty");
float $rnY = getAttr ($myObj+".randY");
float $crZ = getAttr ($myObj+".tz");
float $rnZ = getAttr ($myObj+".randZ");

vector $currPos = <<$crX,$crY,$crZ>>;
vector $targetPos = <<$rnX,$rnY,$rnZ>>;
//compute velocity
vector $targetVelocity = $targetPos-$currPos;
vector $targetUnitVelocity = unit($targetVelocity);
float $targetDist = mag($targetVelocity);

//if current velocity distance is less than speed calculate new pos
if($targetDist<$spd){
//move object to desired pos
setAttr ($myObj+".tx") ($rnX);
setAttr ($myObj+".ty") ($rnY);
setAttr ($myObj+".tz") ($rnZ);
//create new target pos
setAttr ($myObj+".randX") (rand($xMin,$xMax));
setAttr ($myObj+".randY") (rand($yMin,$yMax));
setAttr ($myObj+".randZ") (rand($zMin,$zMax));
}else{
//else move object using vector mult by speed
setAttr ($myObj+".tx") ($crX+($spd*$targetUnitVelocity.x));
setAttr ($myObj+".ty") ($crY+($spd*$targetUnitVelocity.y));
setAttr ($myObj+".tz") ($crZ+($spd*$targetUnitVelocity.z));
}
0 Likes
Message 4 of 4

Anonymous
Not applicable
first off thank you so much, this has been boggling my mind for the longest time.

second this happens after I try running it.

// Error: No object matches name: pCube1.randX //
// Error: An execution error occured in the expression expression1. //
0 Likes