Game Development Community

%this.startRotation = %this.owner.rotation;

by rennie moffat · in Torque Game Builder · 11/09/2009 (1:38 pm) · 4 replies

Hi there I have a question relating to this code. It also relates to a more sweeping question of Torque scripts abilities.


this line

%this.startRotation = %this.owner.rotation;

... contains two elements not in the basic, core language of Torque (at least according to documents) there is no "rotation" or "start rotation", there is however a setAutoRotation and getRotation. I am wondering does this mean the term "rotation" is recognized by the core compiler?


But also more importantly, can I say create my own term, say "fly" and use prefixes accordingly like "setFly", "startFly" etc, and the compiler with recognize "fly" as something I have pre-made (yes) but also recognize the prefixes in front and use its own logic to link the two or am I just dreaming?

Thanks.

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
11/09/2009 (5:44 pm)
There is no way for the compiler to recognize patterns in your code and generate short cuts for you. Any method with setFoo or getFoo will have to be defined by the programmer (you).

Now i must admit i am still new to TS and the following is actually based on C++, or similar Object Oriented languages:

.set and .get methods provide easy, safe, access to the data members of a class. In C++ class inheritance and privacy are very obvious and deliberate. in TS, from what i can tell, they're not so much.

For instance, lets say you had a C++ Class named "Person" and person had an attribute called 'name'. Now how would we access 'name'? If 'name' was a purely public data member, then it would be easy to just call (after instantiating a person object called 'myPerson')

myName = myPerson.name;

but this is bad because we could also change name from outside the class by saying:

myPerson.name = 'John Doe';

which doesn't make sense (in real life, a random person who asks for your name shouldn't be able to change it at will).

Therefore, the better strategy is to make .name a private datamember. but now simply saying:

myName = myPerson.name;

will return an error about being unable to access .name. Good!

now we must write a .getName function!

this function could pretty much be summed up in one line:

return %this.name;

yup. thats it. Now getName will return the value of name without making it vulnerable to change!

Anyway, that was much too long of a digression. Back to your question:

Im not sure where your example comes from, but, startRotation and rotation are simply attributes (variables with a value) of some object.

Assuming the context of a behavior, we can conclude that the BEHAVIOR has a field called 'startRotation'. We can also conclude that the object that the behavior is linked to (its owner) has a field called rotation.

The logic is pretty simple. myBehavior wants to manipulate the rotation of its owner object (%this.owner). Therefore it captures the value (thats the equal sign [=] part..) of %this.owner.rotation and assigns it to a local variable for manipulation (%this.startRotation). Then (presumably) it does some manipulation to %this.startRotation, probably with user input, and will eventually have to change %this.owner.rotation to reflect that manipulation.

All through this process, its important to note that neither .rotation or .startRotation are methods (functions of a class). Even though .startRotation looks similar to .setRotation() [which is a method], it is simply a local variable, not a method.
#2
11/13/2009 (1:09 pm)
%this.owner.rotation

I have been going over using this SetAngularVelocity method,
its 2 variables are %direction and %speed.

%speed works just fine but I want it to rotate and lead like the Tank, Astroid etc and when I use left and right in the %direction, it moves across with no rotation, just the angle i put in (say 30 degrees).


My problem is the tankControls and some others just use %this.owner.rotation in place of %direction which gets it to rotate(spin) which determines its direction of course combined with angular velocity.

The code looks like this.
%this.owner.setImpulseForcePolar(%this.owner.rotation, (%this.up - %this.down) * %this.acceleration);
   %this.owner.setAngularVelocity((%this.right - %this.left) * %this.turnSpeed);





My issue, or question is that "rotation" is the only difference between what I had, and this, but did not work. What I do not understand clearly yet is how "rotation" works. Is it a base call for TGB that is not listed. It is not listed like setRotationAuto etc, it just isn't.

Granted I understand that %rotation is something so...

would others similar and be called as %this.owner.rotaton, would be things like %this.owner.angle, %this.owner.velocity? Im guessing so.


#3
11/13/2009 (2:08 pm)
%this.owner.setLinearVelocityPolar(%this.owner.rotation, (%this.up - %this.down) * %this.moveSpeed);
    %this.owner.setAngularVelocity((%this.right - %this.left) * %this.turnSpeed);


So I have been looking it over.
this.owner.rotation as used in a tank style control.

It seems to me that I am still slightly confused on what it happening here. My thought is that "rotation" is being called from the angularVelcoity ( a command which makes an object spin), however, setAngualrVelocity variable is "velocity", which in the this case is an "angle". No Rotation in the hizzle.

what gives?


#4
11/25/2009 (12:03 pm)
I have been playing with this code. It is the original TGB onMouseDownRotate.cs.
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

if (!isObject(MouseDownRotateBehavior))
{
   %template = new BehaviorTemplate(MouseDownRotateBehavior);
   
   %template.friendlyName = "Mouse Down Rotate";
   %template.behaviorType = "Mouse Input";
   %template.description  = "Rotate the object on mouse down";

   %template.addBehaviorField(rotateSpeed, "The speed at which the object will rotate (degrees per second)", float, 45.0);
   %template.addBehaviorField(resetOnMouseUp, "Reset the rotation when the mouse is released", bool, 0);
}

function MouseDownRotateBehavior::onBehaviorAdd(%this)
{
  
   %this.owner.setUseMouseEvents(true);
}

function MouseDownRotateBehavior::onMouseDown(%this, %modifier, %worldPos)
{
     %this.owner.setAngularVelocity(%this.rotateSpeed);
}

function MouseDownRotateBehavior::onMouseUp(%this,%modifier,%worldPos, %a1, %a2, %a3)
{
   %this.owner.setAngularVelocity(0);
   if(%this.resetOnMouseUp)
      %this.owner.rotation = %this.startRotation;
}

function MouseDownRotateBehavior::onMouseLeave(%this,%modifier,%worldPos, %a1, %a2, %a3)
{
   %this.owner.setAngularVelocity(0);
   if(%this.resetOnMouseUp)
      %this.owner.rotation = %this.startRotation;
}



originally, %this.owner.rotation = %this.startRotation; also existed in onBehaviorAdd and onMouseDown, I removed it in both and it still, the behavior works fine, even with the bool checked.

What I am wondering is and I ask after reading the response to this (@Nick, thanks) is this. This is my logic talking aloid, perhaps if my thinking is wrong my errors could be pointed out by anyone willing to help.



in onBehaviorAdd
%this.owner.rotation = %this.startRotation;
means that the initial position (re:rotation) of the object.owner, is the position of the reset?


so when the bool is called the rotation(the float value) is reset to what it was at onBehvior add?!!!