danni.coy Posted July 15, 2011 Share Posted July 15, 2011 I have been looking at using world expressions a bit of late I thought I might start sharing some recipies. this is the first one Mover Node structure looks like this + WorldExpression (Mover) -+- Node +- NodeDummy +- NodeDummy +- .... To store abitary data that a WorldExpression can access that isn't generated every update cycle we can give our object a custom property. mover.prop looks something like this <?xml version="1.0" encoding="utf-8"?> <properties version="1.00"> <property editable="0" name="Mover"> <parameter name="current target" flags="expand">1</parameter> </property> </properties> The code that the world expression runs looks something like this. enum{CURRENT_TARGET=0,}; float min_dist = 0.05f; float speed = 1.0f; Node node = getNode(); Property p = node.getProperty(); if (p == 0) return; int num = node.getNumChilds()-1; if (num < 1) return; Node mover = node.getChild(0); int current_target = p.getParameterInt(CURRENT_TARGET); if (current_target < 1 || current_target >= node.getNumChilds()) current_target = 1; Node target = node.getChild(current_target); float dist = length(target.getPosition()-mover.getPosition()); if(dist < min_dist) { current_target++; if (current_target > num)current_target = 1; p.setParameterInt(CURRENT_TARGET,current_target); target = node.getChild(current_target); } float k = (engine.game.getIFps()/dist)*speed; // mover.setTransform(lerp(mover.getTransform(),target.getTransform(),k)); mover.setPosition(lerp(mover.getPosition(),target.getPosition(),k)); mover.setRotation(lerp(mover.getRotation(),target.getRotation(),k)); // // mover.setScale(lerp(mover.getScale(),target.getScale(),k)); how it works The world expression assumes that it's first child is the object to be moved and that all the other objects are targets. The current target starts off being the first target (child 1) and the moving object moves towards it using linear interpolation. once this object is reached. The second target becomes the current target and so on. Link to comment
Recommended Posts