Author Topic: [Help] Leveling System, Code on Spawn, Schedules, Teleportation  (Read 4625 times)

Here's my code:
            %point = posFromRayCast(%result);
            //Extracting the coordinates, so we can teleport the player there.
            %xW = getWord(%point,0);
            %yW = getWord(%point, 1);
            %zW = getWord(%point, 2);
            //Teleports player to those coordinates
            %client.player.setTransform(%xW SPC %yW SPC %zW);

Just a note: there is no reason to "extract the coordinates" here. You're getting the first, second, and third word, then passing it to the setTransform function as 3 words.

It's easier to do this: %client.player.setTransform(posFromRaycast(%result));

I probably should have explained how vectorAdd and vectorScale work. These are two of a handful of vector math functions that torque has by default, and certainly the most applicable.

As I'm sure you know, vectors in torque are represented as strings, where the components of the vector are separated by spaces. While separating the vector into its components and doing math on the individual components works, you're sort of reinventing the wheel, since vectorAdd and vectorScale can do this work for you as well as make your code easier to read.

This is essentially the coding for vectorAdd, although it's probably implemented in C++ and not torque, and can probably handle vectors of length >3:
Code: [Select]
function vectorAdd(%A, %B)
{
%xa = getWord(%A, 0);
%ya = getWord(%A, 1);
%za = getWord(%A, 2);
%xb = getWord(%B, 0);
%yb = getWord(%B, 1);
%zb = getWord(%B, 2);
%x = %xa + %xb;
%y = %ya + %yb;
%z = %za + %zb;
return (%x SPC %y SPC %z);
}

Using vectorAdd instead of doing all of the vector math yourself will allow you to reduce this:
Code: [Select]
                    //EyeVector
                    %eyeVector = %player.getEyeVector();
                    %vx = getWord(%eyeVector, 0);
                    %vy = getWord(%eyeVector, 1);
                    %vz = getWord(%eyeVector, 2);
                    //Now for EyePoint
                    %eyePoint = %player.getEyePoint();
                    %px = getWord(%eyePoint, 0);
                    %py = getWord(%eyePoint, 1);
                    %pz = getWord(%eyePoint, 2);
                    //Results!
                    %rx = %px + %vx;
                    %ry = %py + %vy;
                    %rz = %pz + %vz;
                    //Putting it together.
                    %result = %rx SPC %ry SPC %rz;
To this:
Code: [Select]
                    //EyeVector
                    %eyeVector = %player.getEyeVector();
                    //Now for EyePoint
                    %eyePoint = %player.getEyePoint();
                    //Results!
                    //Putting it together.
                    %result = vectorAdd(%eyePoint, %eyeVector);

vectorScale is similar, but instead of taking two vectors, it takes a single vector and what's called a scalar (more commonly known as a plain old number). Its implementation is something like this:
Code: [Select]
function vectorScale(%A, %k)
{
%xa = getWord(%A, 0);
%ya = getWord(%A, 1);
%za = getWord(%A, 2);
%xa *= %k;
%ya *= %k;
%za *= %k;
return (%xa SPC %ya SPC %za);
}

This works beautifully (cough), except for the fact that rotation is not taken into account. I'm gonna continue snooping around.
If by rotation you mean the direction of the projectile, take the player's eyeVector and scale it by the speed you want the projectile to move at, and use that as the initial velocity.

Thanks! The explanations really helped.