still need help with pinball functionality
by Jon Rothstein · in Torque Game Builder · 07/23/2009 (3:59 pm) · 6 replies
Where can I find help creating a pinball machine game with TGB. Is there any published info or add-ons for this type of game. I especially need help with flippers. Thanks.
Jon
Jon
#2
Great code that helps plenty thanks for posting. One question, how would you go about actually LAUNCHING the ball, say with the down arrow key and releasing to launch the ball upwards? Any idea on that cause I'm stumped. Thanks for any help.
Will
01/16/2011 (5:44 am)
Tom,Great code that helps plenty thanks for posting. One question, how would you go about actually LAUNCHING the ball, say with the down arrow key and releasing to launch the ball upwards? Any idea on that cause I'm stumped. Thanks for any help.
Will
#3
Make chargingBall and releaseBall functions and bind them to the down button down and up. I forget how you get update ticks, but in that update loop for the ball (or shooter thingy) you check a flag that was set by chargingBall, like isCharging, and add to a charge value based on the time. Then, when releaseBall goes off, turn off the flag and set the velocity of the ball based on the charge value.
Hope that helps!
01/16/2011 (5:51 pm)
Yo Will, haven't done any TGB stuff in a bit, but I'd probably do it like this:Make chargingBall and releaseBall functions and bind them to the down button down and up. I forget how you get update ticks, but in that update loop for the ball (or shooter thingy) you check a flag that was set by chargingBall, like isCharging, and add to a charge value based on the time. Then, when releaseBall goes off, turn off the flag and set the velocity of the ball based on the charge value.
Hope that helps!
#4
01/16/2011 (11:19 pm)
Something like this should work. I didn't test with Tom's code but I read this post and got the urge to try it out. It's an obnoxious use of globals but using the bindObj way of things is more complicated... eh.function Launcher::onLevelLoaded(%this, %scenegraph)
{
echo("Launcher loaded");
//setup some params
$MIN_HEIGHT = 10;
$MAX_HEIGHT = 20;
$START_POS_Y = %this.getPositionY();
$MAX_VELOCITY = -1;
$MAX_CHARGE = 100;
$CURRENT_CHARGE = 0;
$CHARGING = false;
%this.enableUpdateCallback();
moveMap.bindCmd(keyboard, space, "startCharging();", "releaseBall();");
}
function startCharging()
{
echo("Starting to charge");
$CHARGING = true;
}
function releaseBall()
{
echo("Relasing ball with charge: " @ $CURRENT_CHARGE);
$CHARGING = false;
MainLauncher.setHeight($MAX_HEIGHT);
MainPinball.setLinearVelocityY($CURRENT_CHARGE * $MAX_VELOCITY);
MainLauncher.setPositionY($START_POS_Y);
}
function Launcher::onUpdate(%this)
{
if($CHARGING)
{
if($CURRENT_CHARGE < $MAX_CHARGE)
{
$CURRENT_CHARGE++;
MainLauncher.setHeight($MAX_HEIGHT - ($CURRENT_CHARGE / $MAX_CHARGE) * $MIN_HEIGHT);
MainLauncher.setPositionY($START_POS_Y + $MAX_HEIGHT - %this.getHeight());
echo("Launcher PosY: " @ MainLauncher.getPositionY());
}
else
{
$CURRENT_CHARGE = $MAX_CHARGE;
}
echo("Launcher charging: " @ $CURRENT_CHARGE);
}
else
{
$CURRENT_CHARGE = 0;
}
}Download TGB project.
#5
Will
01/17/2011 (3:57 am)
Sweet, that should get me rolling in the right direction. Thanks for the help and posting the code and the demo. Just trying to make the pinball game as fast as I can and this helps. Now going to the lab to put all the puzzle together and see if i can not bring it all to life. :D Thanks!Will
#6
cough... cough...
01/17/2011 (2:14 pm)
No problem. It was a fun little exercise to go through while I anxiously fill the space and buoy myself up while waiting for someone who shall not be named to release their game.cough... cough...
Associate Tom Eastman (Eastbeast314)
Flipper code:
//handles the two flippers, differentiating // between them by their names (this lets you // add one of those min-flippers too) moveMap.bindCmd(keyboard, "a", "leftFlipper.flip();", "leftFlipper.unFlip();"); moveMap.bindCmd(keyboard, "l", "rightFlipper.flip();", "rightFlipper.unFlip();"); function Flipper::onLevelLoaded(%this, %scenegraph) { //Flippers are like walls, they receive only %this.setCollisionActive( false, true ); %this.setCollisionResponse("RIGID"); //save the initial rotation %this.startRot = %this.getRotation(); //allow dynamic field to override this if( %this.speed $= "") %this.speed = 1000; } function Flipper::flip(%this) { //tell the flippers to rotate if( %this $= rightFlipper ) %this.rotateTo( 90, %this.speed); else if( %this $= leftFlipper ) %this.rotateTo( 270, %this.speed); else echo("Flipper named \""@%this.getName()@"\" is not recognized!"); } function Flipper::unFlip(%this) { %this.rotateTo( %this.startRot, %this.speed); }Ball Code
//handles the ball, mainly just initialization //______________________________________________________________ //General explanation of how the collision and physics are setup // //Collisions //The ball is set to send and not receive collisions. // It's the only object setup like that. All other objects // that it can collide with only recieve collisions. // This means that walls won't collide with other walls // and so on. I think it would also work if the settings // were reversed (ball receives, everything else sends). // //Physics //The ball is set to both send and receive physics. // That doesn't really matter in this case (as long // as it has one of them checked, it'll interact with // things that have the other option checked). // The key, however, is to have the walls send but not // receive in this case, so that the ball bounces off // of them but they aren't moved by the ball. function PinBall::onLevelLoaded(%this, %scenegraph) { //The ball will send collisions, but not receive %this.setCollisionActive( true, false ); //It's a rigid body %this.setCollisionResponse("RIGID"); //The circle collision mode is wonky, use a // 64-sided n-gon instead %this.setCollisionPolyPrimitive( 64 ); //gravity %this.setConstantForce( 0, 90, true); //A little less bouncy - this was the only tweak I did // Still feels floaty %this.setRestitution( 0.8 ); %this.setCollisionPhysics( true, false ); } function PinBall::springStart(%this) { //A nice little vertical boost! %this.setLinearVelocity( 0, -300 ); }Now, let's see what I remember. The flippers were images with the flipper in the corner of the image so that the center of the image is the rotation point of the flipper. They were named and classed as in the code bindings. They definitely need a collision poly.
The pinball itself just needs to be classed, it looks like. I made some walls with receive collision, I suppose...
If you need some clarification or more help, let me know. Hope this gets you started!