Bouncing Vectors
by Douglas Koup · in Torque Game Builder · 07/04/2006 (10:18 am) · 9 replies
I realized I had this in the wrong forum. Moving here.
Here is my code to figure out how a player's velocity vector would bounce when it collides with a surface.
if(%dstObj.class $= "Solid")
{
%newVector = $PlayerOne.getLinearVelocity() - 2 * %normal * (t2dVectorDot(%normal, $PlayerOne.getLinearVelocity()));
}
I haven't had a chance to test it yet, which brings me to my main issue. The result of that equation should be a vector that represents the new path of my player. I can getPlayerVelocity() to get the original in vector form but I can't setPlayerVelocity(*vector*) and can't really see another way to do it. If, for some reason, there is no convenient way to set a vector, what would you all recommend for converting it to a usable form? Either (%x, %Y) or (%direction, %speed) will work for me.
Here is my code to figure out how a player's velocity vector would bounce when it collides with a surface.
if(%dstObj.class $= "Solid")
{
%newVector = $PlayerOne.getLinearVelocity() - 2 * %normal * (t2dVectorDot(%normal, $PlayerOne.getLinearVelocity()));
}
I haven't had a chance to test it yet, which brings me to my main issue. The result of that equation should be a vector that represents the new path of my player. I can getPlayerVelocity() to get the original in vector form but I can't setPlayerVelocity(*vector*) and can't really see another way to do it. If, for some reason, there is no convenient way to set a vector, what would you all recommend for converting it to a usable form? Either (%x, %Y) or (%direction, %speed) will work for me.
#2
I still have that other issue though...what do I do with the vector once I get it?
For the incoming object I can get the information as a:
- vector - getLinearVelocity()
- speed, angle - getLinearVelocityPolar()
For the surface I can get the information as a:
- vector - %normal
I can assign the results using:
- x and y component - setLinearVelocity(%x, %y)
- speed, angle - setLinearVelocity(%direction, %speed)
Soooo, that leaves me either missing the way to easily assign the resulting vector or having to do some conversion. Only problem is, I see no way to conveniently convert the vector. I feel I must be missing something obvious.
Perhaps I could use t2dAngleBetween(%vector1, %vector2), convert it to get the angle of incidence, double it, and then add it to the angle from getLinearVelocityPolar() to get my end result. I will test it out after I get some sleep.
07/05/2006 (9:13 am)
Awesome! Thanks for the help.I still have that other issue though...what do I do with the vector once I get it?
For the incoming object I can get the information as a:
- vector - getLinearVelocity()
- speed, angle - getLinearVelocityPolar()
For the surface I can get the information as a:
- vector - %normal
I can assign the results using:
- x and y component - setLinearVelocity(%x, %y)
- speed, angle - setLinearVelocity(%direction, %speed)
Soooo, that leaves me either missing the way to easily assign the resulting vector or having to do some conversion. Only problem is, I see no way to conveniently convert the vector. I feel I must be missing something obvious.
Perhaps I could use t2dAngleBetween(%vector1, %vector2), convert it to get the angle of incidence, double it, and then add it to the angle from getLinearVelocityPolar() to get my end result. I will test it out after I get some sleep.
#3
Vectors in TS are stored as strings. So a vector thats (4, 2) is "4 2" in TS. To access these numbers, you use getWord(). So if the example vector is x and y, you use
And to write some code for you,
The above example can be cleaned up a little too:
Again, I have not tested this code.
07/05/2006 (9:25 am)
It looks like you need to do some basic documentation review. Go through some of the tutorials, they'll do you a world of good.Vectors in TS are stored as strings. So a vector thats (4, 2) is "4 2" in TS. To access these numbers, you use getWord(). So if the example vector is x and y, you use
%x = getWord(%vector, 0); %y = getWord(%vector, 1);
And to write some code for you,
$PlayerOne.setLinearVelocity( getWord(%newVector, 0), getWord(%newVector, 1) );
The above example can be cleaned up a little too:
%v = $PlayerOne.getLinearVelocity(); %nDv = 2.0 * t2dVectorDot(%normal, %v ); %newVector = t2dVectorSub(%v, t2dVectorScale( %normal , %nDv )); $PlayerOne.setLinearVelocity( getWord(%newVector, 0), getWord(%newVector, 1) );
Again, I have not tested this code.
#4
One would assume that I could immediatly pull the information back out as a vector once I have gone through the effort of converting it to an assignable format. I just thought that was odd.
07/05/2006 (9:48 am)
I understand that I can pull out the information with getWord, I was just wondering why I can get every bit of information in vectors but could not assign simply with a vector. I assumed I was missing something.One would assume that I could immediatly pull the information back out as a vector once I have gone through the effort of converting it to an assignable format. I just thought that was odd.
#5
07/05/2006 (9:55 am)
I agree, it is odd. But that's the way it is. In this case, I think it's poor design that the getLinearVelocity returns a vector string and the setLinearVelocity takes two parameters. It is easier to do thissetLinearVelocity(%x SPC %y);than it is to use getWord blah blah blah. It also looks cleaner. And it has a direct connection to getLinearVelocity and all the vector math. But oh well. It is still a good engine... it just feels a little awkward at times.
#6
Heck, I'm almost done replacing the physics engine using only script =P
07/05/2006 (10:02 am)
I do like the engine, and especially the scripting engine.Heck, I'm almost done replacing the physics engine using only script =P
#7
If you look at t2dSceneObject.cc in the setLinearVelocity's ConsoleMethod, you'll see that it looks at the first argument. If that argument contanis two words, it knows the first parameter is a vector. If that argument contains only one word, it assumes that the first argument is the x-component and the second is the y-component.
07/05/2006 (11:10 am)
I am certain that setLinearVelocity can take a vector. I do it all over the place.If you look at t2dSceneObject.cc in the setLinearVelocity's ConsoleMethod, you'll see that it looks at the first argument. If that argument contanis two words, it knows the first parameter is a vector. If that argument contains only one word, it assumes that the first argument is the x-component and the second is the y-component.
#8
I guess my question is: Are the built in vector calculation methods faster than performing all the calculations yourself using the math functions? I don't know, I'm still new to all this stuff. Vectors are a big deal in physics, and when I get to that I expect this will be an issue. I just don't understand why I would use these built in functions rather than my own.
07/07/2006 (9:04 am)
My question is somewhat related to this thread, so I will post it here. I've been wondering: What are the advantages to using built in vectors rather than calculating them yourself? Doing it yourself would result is less code and it would be a lot cleaner from the looks of things. Vectors are just an angle and a magnitude, converting it to x and y coordinates are as simple as sin and cos with a little arctan, and some arccos/arcsin for xy to vector conversions. I guess my question is: Are the built in vector calculation methods faster than performing all the calculations yourself using the math functions? I don't know, I'm still new to all this stuff. Vectors are a big deal in physics, and when I get to that I expect this will be an issue. I just don't understand why I would use these built in functions rather than my own.
#9
You can do the vector calculations yourself in script. If you want it to look clean, you should keep each portion of the vector (either the x and y or the angle and radius) in separate variables. I, on the other hand, like to keep my vectors in one string (as "x y"). The built-in functions can handle it either way.
Sometimes I plan on doing a long, complicated calculation. In those cases, I add a ConsoleFunction to the source code. While the scripting is quite fast, it is slightly faster to have it done in code and it keeps my scripts smaller.
07/07/2006 (10:02 am)
While there are some functions that take polar coordinate vectors, most take (x,y) vectors.You can do the vector calculations yourself in script. If you want it to look clean, you should keep each portion of the vector (either the x and y or the angle and radius) in separate variables. I, on the other hand, like to keep my vectors in one string (as "x y"). The built-in functions can handle it either way.
Sometimes I plan on doing a long, complicated calculation. In those cases, I add a ConsoleFunction to the source code. While the scripting is quite fast, it is slightly faster to have it done in code and it keeps my scripts smaller.
Torque Owner J. Alan Atherton
Note that I did not test this code... you may need to do some debugging.
The moral of the story is that there are no vector operators in torquescript... only vector functions. So if you want to add vectors, skip the + operator and use t2dVectorAdd.