Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / MEL / Random Motion Path Code?
  RSS 2.0 ATOM  

Random Motion Path Code?
Rate this thread
 
51524
 
Permlink of this thread  
avatar
  • frujo23
  • Posted: 20 January 2011 09:24 AM
  • Total Posts: 41
  • Joined: 19 June 2008 04:59 AM

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!



Replies: 0
avatar
  • raym4n
  • Posted: 02 February 2011 09:49 PM

//- 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.



Replies: 1
/img/forum/dark/default_avatar.png

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. //

Author: frujo23

Replied: 14 March 2011 09:39 AM  
avatar
  • raym4n
  • Posted: 02 February 2011 10:13 PM

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));
}


Replies: 0