Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Animate transform change with scripting

hadoken
Guest
Quote
2021-03-14 12:17:44

Here's a small handy action script of mine to copy the absolute (= global) Position from one object to another e.g. for creating a pseudo-parent-child-relationship in combination with the built-in "every-few-seconds-do-something" behavior):

===============================================



/* <action jsname="action_CopyPositionAbs" description="Copy absolute Position">
<property name="Child" type="scenenode"/>
<property name="Parent" type="scenenode"/>
</action>
*/

action_CopyPositionAbs = function()
{
};

// called when the action is executed
action_CopyPositionAbs.prototype.execute = function(currentNode)
{
ccbSetSceneNodeProperty(this.Child, "Position", ccbGetSceneNodeProperty(this.Parent, "PositionAbs"));
}



===============================================



I'm looking for a solution to be added to this script for how to animate the position change of the pseudo-child over time.

So if you know of the right "while/for/loop" or other best suiting commands in CC javascript, please let me & others know. THX


smnmhmdy
Registered User
Quote
2021-03-14 15:31:13

Hey @hadoken, Assuming I understood what you mean, This could animate the movement of a node (Child) to another node's (Parent) position
/* <action jsname="action_CopyPositionAbs" description="Copy Absolute Position Animated">
<property name="Child" type="scenenode"/>
<property name="Parent" type="scenenode"/>
<property name="MoveSpeed" type="float" default="5"/>
</action>
*/

var cpaRegister; // an object containing the list of registered nodes and their states

action_CopyPositionAbs = function(){
this.lastTime = new Date().getTime();
if(!cpaRegister) cpaRegister = {};
}

action_CopyPositionAbs.prototype.execute = function(){
this.childName = ccbGetSceneNodeProperty(this.Child, "Name");
var childPosition = ccbGetSceneNodeProperty(this.Child, "Position");
var targetPosition = ccbGetSceneNodeProperty(this.Parent, "PositionAbs");
var distance = childPosition.substract(targetPosition).getLength();

// if the distance is short enough don't register a new frame event and set the position
if(Math.round(distance) <= this.MoveSpeed * 0.1){
ccbSetSceneNodeProperty(this.Child, "Position", targetPosition);
return;
}

if(cpaRegister[this.childName]) return; // node is already registered

// registering a new frame event for the child node
var cpa = this;
this._Update = function(){ cpa.Update(); };
ccbRegisterOnFrameEvent(this._Update);
cpaRegister[this.childName] = true; // setting this child's flag to true to prevent multiple frame events on the same node
}

action_CopyPositionAbs.prototype.Update = function(){
// time
this.absTime = new Date().getTime();
this.deltaTime = this.absTime - this.lastTime;
this.lastTime = this.absTime;

var childPosition = ccbGetSceneNodeProperty(this.Child, "Position");
var targetPosition = ccbGetSceneNodeProperty(this.Parent, "PositionAbs");
var movementVect = childPosition.substract(targetPosition);
var distance = movementVect.getLength();
movementVect.normalize();
movementVect.x *= this.MoveSpeed * this.deltaTime * 0.01;
movementVect.y *= this.MoveSpeed * this.deltaTime * 0.01;
movementVect.z *= this.MoveSpeed * this.deltaTime * 0.01;
childPosition = childPosition.substract(movementVect);
if(Math.round(distance) > this.MoveSpeed * this.deltaTime * 0.01){
ccbSetSceneNodeProperty(this.Child, "Position", childPosition);
} else {
ccbSetSceneNodeProperty(this.Child, "Position", targetPosition);
ccbUnregisterOnFrameEvent(this._Update); // target reached, unregistering this event
cpaRegister[this.childName] = false; // setting node's registered state to false
}
}



hadoken
Guest
Quote
2021-03-14 17:23:47

Thanks a lot @smnmhmdy, that's exactly what I've been looking for! :-)

Since we now have nice 3rd person controllers I'm focusing on platforms on which the player can move along with as another aspect of game mechanics development & level design.

Up to now there are only a few but unsatisfying examples to be found for the subject. My latest experiments in this field are promising and can hopefully be enhanced with your given scripting support. I think I'll open a specific thread for it soon...


smnmhmdy
Registered User
Quote
2021-03-14 19:46:02

I'm glad you found it useful.

And if you mean a platform which the player can stand on and move with then I've done something like that (https://www.dropbox.com/s/5h2b51...), so I'm sure I could be a little help on the matter :)


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "?nternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact