Bouncing Ball Code
by rennie moffat · in Torque Game Builder · 08/22/2009 (12:25 pm) · 7 replies
Hi this code os from the bouncing ball code.
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
I have some questions regarding it that are puzzling me.
a. why onMouseDown, would he setLinearVelocity? Then again, he declares it in onMouseDragged, except it is set to ("0. 0"). I would have thought a (%x, %y) would have been better since the velocity changes (is variant on mouse movement)
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
I have some questions regarding it that are puzzling me.
a. why onMouseDown, would he setLinearVelocity? Then again, he declares it in onMouseDragged, except it is set to ("0. 0"). I would have thought a (%x, %y) would have been better since the velocity changes (is variant on mouse movement)
function BounceBall::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
// reset linear and angular velocity
%this.setLinearVelocity("0 0");
%this.setAngularVelocity(0);
// set the collision response to CLAMP and make it immovable temporarily
%this.setCollisionResponse("CLAMP");
%this.setImmovable(true);
// make this dragable
sceneWindow2D.draggingBall = %this;
// store temp time/pos and mouse offset
%this.tmpTime = getSimTime();
%this.tmpPos = %this.getPosition();
%this.mouseOffset = t2dVectorSub(%this.tmpPos, %worldPosition);
}
function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if(isObject(%this.draggingBall))
{
// reset linear and angular velocity
%this.draggingBall.setLinearVelocity("0 0");
%this.draggingBall.setAngularVelocity(0);
// get tmp pos and update last pos
%this.draggingBall.lastPos = %this.draggingBall.tmpPos;
%this.draggingBall.tmpPos = %this.draggingBall.getPosition();
// get tmp time and update last time
%this.draggingBall.lastTime = %this.draggingBall.tmpTime;
%this.draggingBall.tmpTime = getSimTime();
// move draggingBall to mouse pos + offset
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
}
}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.
#2
But the functions declared class is
BounceBall::
so, sceneWindow2D is the related to the draggingBall here...
sceneWindow2d.draggingBall = %this;
then
we have sceneWindow2D as a class in the onMouseDragged Function...
where the object %this.dragginBall is declared in an if statement ...
if (isObject(%this.draggingBall) ...
where the dragging balls properties are declared...
%this.draggingBall.setLinearVelocity("0 0");
%this.draggingBall.setAngularVelocity(0);
...
%this.DragingBall.lastPos = %this.draggingBall.tmpPos;
%this.dragginBall.tmpTime = getSimTime();
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
}end
the only question I have is...
the last line...
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
Here I am setting the position of the "vectorAdd" I Looked it up in ref
http://tdn.garagegames.com/wiki/TorqueScript_Commands#VectorMath
it has all types of basic math functions for instance at its more basic levels it has vectorAdd, Sub, Scale.
So when I add VectorAdd, it is adding what precisely. This would be where I would be confused on.
08/24/2009 (12:50 pm)
sceneWindow2D.draggingBall = %this; ///is inside the mouseDown Function.But the functions declared class is
BounceBall::
so, sceneWindow2D is the related to the draggingBall here...
sceneWindow2d.draggingBall = %this;
then
we have sceneWindow2D as a class in the onMouseDragged Function...
where the object %this.dragginBall is declared in an if statement ...
if (isObject(%this.draggingBall) ...
where the dragging balls properties are declared...
%this.draggingBall.setLinearVelocity("0 0");
%this.draggingBall.setAngularVelocity(0);
...
%this.DragingBall.lastPos = %this.draggingBall.tmpPos;
%this.dragginBall.tmpTime = getSimTime();
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
}end
the only question I have is...
the last line...
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
Here I am setting the position of the "vectorAdd" I Looked it up in ref
http://tdn.garagegames.com/wiki/TorqueScript_Commands#VectorMath
it has all types of basic math functions for instance at its more basic levels it has vectorAdd, Sub, Scale.
So when I add VectorAdd, it is adding what precisely. This would be where I would be confused on.
#3
// move draggingBall to mouse pos + offset
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
I am confused here.
I am moving the dragging ball to mouse position. OK.
But what about the offset? This is the original mouse position?
///Offset is defined as the short, perpendicular distance from which the main line of measurement is made.
So this is used to better the movement of the ball? Not entirely sure. Specifically why he put in mouseOffset.
08/24/2009 (1:08 pm)
so specifically...// move draggingBall to mouse pos + offset
%this.draggingBall.setPosition(t2dVectorAdd(%worldPosition, %this.draggingBall.mouseOffset));
I am confused here.
I am moving the dragging ball to mouse position. OK.
But what about the offset? This is the original mouse position?
///Offset is defined as the short, perpendicular distance from which the main line of measurement is made.
So this is used to better the movement of the ball? Not entirely sure. Specifically why he put in mouseOffset.
#4
08/24/2009 (1:18 pm)
t2dVectorAdd just adds the two vectors based off of their coordinates on the grid (for instance (3, 5) and (1, 4)). The first is probably where the ball is currently at, and the second is probably how far the mouse is from the ball, then it just adds the two and sets the new position of the ball to that new location. I can't be sure because I haven't done that tutorial before.
#5
well here is the tut.
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
so say the points are 3, 5 and 1, 4
(3, 5) + (1,4) = (4, 9)
ehn, i just looked at this graphically thinking 3, 5 is x and y.
or is 3, 5 actually X1, X2?
I looked at it on an x y and a plain old flat chart, one axis. One correlation seems more linear. X Y seems more less predictable. Not sure tho.
08/24/2009 (1:33 pm)
ok, hmm, well here is the tut.
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
so say the points are 3, 5 and 1, 4
(3, 5) + (1,4) = (4, 9)
ehn, i just looked at this graphically thinking 3, 5 is x and y.
or is 3, 5 actually X1, X2?
I looked at it on an x y and a plain old flat chart, one axis. One correlation seems more linear. X Y seems more less predictable. Not sure tho.
#6
well here is the tut.
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
so say the points are 3, 5 and 1, 4
(3, 5) + (1,4) = (4, 9)
ehn, i just looked at this graphically thinking 3, 5 is x and y.
or is 3, 5 actually X1, X2?
I looked at it on an x y and a plain old flat chart, one axis. One correlation seems more linear. X Y seems more less predictable. Not sure tho. No Matter.
08/24/2009 (1:35 pm)
ok, hmm, well here is the tut.
tdn.garagegames.com/wiki/TGB/MiniTutorials/ObjectMouseEvents
so say the points are 3, 5 and 1, 4
(3, 5) + (1,4) = (4, 9)
ehn, i just looked at this graphically thinking 3, 5 is x and y.
or is 3, 5 actually X1, X2?
I looked at it on an x y and a plain old flat chart, one axis. One correlation seems more linear. X Y seems more less predictable. Not sure tho. No Matter.
#7
The one that is most relevant for this code is the "class = BounceBall;" line. The t2dStaticSprite is now a BounceBall. So every t2dStaticSprite created with BounceBallDatablock is now a new type: BounceBall.
The first function is "BounceBall::onLevelLoaded". This function will be called for every object of type BounceBall when the level is loaded (e.g. when the game starts) and the specific instance will be called "%this". It's a little silly, but this function first tells the scene window to use mouse events. After that, it tells this ball to use mouse events, too.
The second function is "BounceBall::onMouseDown". This only gets called because we told every BounceBall to listen to mouse events. When a BounceBall has the left mouse button pressed down over it, this function is called for the specific ball that was clicked on (signified with the variable "%this"). This ball becomes the scene's "draggingBall". You'll see at the bottom that there is a dynamic variable created in the BounceBall called mouseOffset. It is the difference between the ball's position and the position of the mouse.
Offsets while dragging are important to create the appearance of grabbing the object at a specific location. A lot of games won't create an offset, but will simply move the center of the object right to where the mouse is located. By keeping track of the offset, we can use it in the future to keep the mouse at the same location on the object as we drag it around.
The next function, "sceneWindow2D::onMouseDragged" does everything I mentioned in my first post. At the bottom, is where you had your next question. "%worldPosition" is a "X Y" coordinate of where the mouse is located. (A simplification, but it will work for now.) The offset that was stored in the draggingBall is added to that. This is the magic that keeps the mouse at the same position on the ball where you click it.
You should note that since the BounceBall is also using mouse events, that you could also write a function "BounceBall::onMouseDragged" with the same parameters as the scene window version.
Hope that helps!
08/24/2009 (2:30 pm)
When you use the BounceBallDatablock to create a t2dStaticSprite (which is what I assume that tutorial does), it sets the properties of the t2dStaticSprite to those of the datablock.The one that is most relevant for this code is the "class = BounceBall;" line. The t2dStaticSprite is now a BounceBall. So every t2dStaticSprite created with BounceBallDatablock is now a new type: BounceBall.
The first function is "BounceBall::onLevelLoaded". This function will be called for every object of type BounceBall when the level is loaded (e.g. when the game starts) and the specific instance will be called "%this". It's a little silly, but this function first tells the scene window to use mouse events. After that, it tells this ball to use mouse events, too.
The second function is "BounceBall::onMouseDown". This only gets called because we told every BounceBall to listen to mouse events. When a BounceBall has the left mouse button pressed down over it, this function is called for the specific ball that was clicked on (signified with the variable "%this"). This ball becomes the scene's "draggingBall". You'll see at the bottom that there is a dynamic variable created in the BounceBall called mouseOffset. It is the difference between the ball's position and the position of the mouse.
Offsets while dragging are important to create the appearance of grabbing the object at a specific location. A lot of games won't create an offset, but will simply move the center of the object right to where the mouse is located. By keeping track of the offset, we can use it in the future to keep the mouse at the same location on the object as we drag it around.
The next function, "sceneWindow2D::onMouseDragged" does everything I mentioned in my first post. At the bottom, is where you had your next question. "%worldPosition" is a "X Y" coordinate of where the mouse is located. (A simplification, but it will work for now.) The offset that was stored in the draggingBall is added to that. This is the magic that keeps the mouse at the same position on the ball where you click it.
You should note that since the BounceBall is also using mouse events, that you could also write a function "BounceBall::onMouseDragged" with the same parameters as the scene window version.
Hope that helps!
Associate William Lee Sims
Machine Code Games
If a ball gets a "onMouseDown" event, then the current ball's velocity is set to a zero-length vector (in effect, stopping it). Then this ball is assigned to the dynamic variable "draggingBall" in the scene window.
When the scene window gets a "onMouseDragged" event, the code is reinforcing the zero velocity. It then moves the "draggingBall" to the position of the mouse. You should also note that it is creating another dynamic variable in the ball called "tmpPos" which is saving where the ball last was.
Finally, when the scene window gets a "onMouseUp" event, it takes the draggingBall's previous position and subtracts it from the mouse's current position (and then it scales this vector up). The draggingBall is then assigned this vector as its velocity.