Applying gravity to shooter demo, seems weird
by Kebab · in Torque Game Builder · 10/26/2006 (2:05 am) · 7 replies
I tried the shooter tutorial for the tgb. Then i wanted to apply some gravity to it. If i press the 'w' button to go up it now only seems to have a one time effect like a push, then the ship starts falling regardless if i keep pressing the 'w' button. I dont quite understand this, shouldnt the effect from the setVelocityY() function make it so that it only moves slower upwards with the gravity effect, instead of a one time push and then start falling?. I mean if you press the 'a' or 'd' buttons to move the ship left or right if still works if you hold the button down?
Is there something i don't understand about the gravity in TGB or?
Is there something i don't understand about the gravity in TGB or?
#2
Is this the way to do it or?
10/28/2006 (7:13 am)
Thanks alot for the help, but what do you mean with reschedule? I tried using the schedule function and set it so that it runs the updatemovement every 100 ms after you have clicked a 'w' button, but i just can't get it to work. And the console doesnt output any error. Is this the way to do it or?
#3
I trying to make a gravity force like game, the ship as of now flys around and you can turn and stuff. but i cant for the life of me understand how to make i fly constanly in one direction with gravity turned on.
10/28/2006 (12:06 pm)
Hmm seems that the schedule() function only does a one time event and not constantly. Bah!I trying to make a gravity force like game, the ship as of now flys around and you can turn and stuff. but i cant for the life of me understand how to make i fly constanly in one direction with gravity turned on.
#4
By reschedule, I meant add a schedule at the end of updateMovement that calls the function again in 100 ms or something. You could also do it with a timer. That's probably easier for this, anyway. Check in the documentation folder for TGB Reference and find the timer stuff. Basically, you turn the timer on for the object and it gives you the onTimer callback every x ms. You would then have that call updateMovement and remove it from the individual keypress functions (so that updateMovement is only called every 100 ms and not then and when one of the keys is pressed or released). Make sense?
10/28/2006 (12:25 pm)
Aight - I looked at the Shooter Tutorial movement code. There might be some complications that would suggest that you should use forces instead of velocity to move, but I'll ignore that and see if the obvious solution works.By reschedule, I meant add a schedule at the end of updateMovement that calls the function again in 100 ms or something. You could also do it with a timer. That's probably easier for this, anyway. Check in the documentation folder for TGB Reference and find the timer stuff. Basically, you turn the timer on for the object and it gives you the onTimer callback every x ms. You would then have that call updateMovement and remove it from the individual keypress functions (so that updateMovement is only called every 100 ms and not then and when one of the keys is pressed or released). Make sense?
#5
I got it working with running a scheduling function inside the scheduling function. Ill check on that timer thing though, seems easier.
Moving on to acceleration then :)
10/28/2006 (2:20 pm)
Thanx m8! I got it working with running a scheduling function inside the scheduling function. Ill check on that timer thing though, seems easier.
Moving on to acceleration then :)
#6
10/28/2006 (4:04 pm)
Great!
#7
As a less-cpu-intensive alternative, consider using the setConstantForce() method. Add in playerShip::onLevelLoaded a pair of globals: $gravityY and $gravityX (in case of a passing neutron star, natch), and set the $gravityY to 20.0, and two member variables: %this.consX = 0.0 and %this.consY = 0.0.
I then replaced the setVelocityY calls in the updateMovement method with %this.consY = +/-50.0 depending on the direction (with the negative number being upwards) and added:
to the end. Don't forget to set the consY to 0 when neither up nor down are being pressed!
If you're doing the kind of game I think you are, you should take a look at the Polar versions of these methods, as they might be good for applying directional accelerations from blackholes and such.
HTH,
--B
10/31/2006 (7:24 pm)
Hi chaps -As a less-cpu-intensive alternative, consider using the setConstantForce() method. Add in playerShip::onLevelLoaded a pair of globals: $gravityY and $gravityX (in case of a passing neutron star, natch), and set the $gravityY to 20.0, and two member variables: %this.consX = 0.0 and %this.consY = 0.0.
I then replaced the setVelocityY calls in the updateMovement method with %this.consY = +/-50.0 depending on the direction (with the negative number being upwards) and added:
%this.setConstantForce(%this.consX + $gravityX, %this.consY + $gravityY, false);
to the end. Don't forget to set the consY to 0 when neither up nor down are being pressed!
If you're doing the kind of game I think you are, you should take a look at the Polar versions of these methods, as they might be good for applying directional accelerations from blackholes and such.
HTH,
--B
Associate Tom Eastman (Eastbeast314)
Anyway, just take a look at how updateMovement works and is called and it should be apparent that it's only called when a button is pressed or released, not continuously.
Hope that helps!