Game Development Community

Water buoyancy

by Shawn LeBlanc · in Torque Game Builder · 02/12/2007 (8:27 am) · 17 replies

I'd like to be able to pull off an effect where (say) a falling barrel lands into water, sinks a bit due to the impact but then starts to float back to the surface where it just bobs. I'm not sure how to setup the barrel and the water tiles to do this effect, if it's even possible. Any suggestions?

Thanks!
Shawn

#1
02/12/2007 (3:00 pm)
@Shawn, off the top of my head, I see a number of possible things you could ...

you used the word "tiles", so I'll assume that your using a tile-map, for possibly an adventure-style game ...

The barrel dropping and 'bobbing' could be done with Sprite Animations ... at which point, your completely free to do whatever you want ...

Alternatively, the drop and bob could be done with a DTS Object, and to simulate the 'drop' you could simply resize the object a few times ... then for the bob, play with the rotations and size again ...

I'd probably stick with a pre-made Sprite Animation though ... all depending on what exactly your trying to pull off, and in what context your doing it ...
#2
02/12/2007 (3:24 pm)
@Shawn - is the falling happening in a side-view like a platformer, or a top-down view as David suggests, or something else?
#3
02/12/2007 (4:21 pm)
Whoops, sorry for not being more specific!

It would be in a side view, 2d scroller style.

I'm just starting out with TGB, so my first idea was to make a "water" tile with specific physics settings (which settings I don't exactly know yet). So when an object, like a barrel (or boat), interacts with it (ie falls into the water/intersects the water tile), the barrel would be moved upwards until reaching the surface of the water tile, where the barrel would start bobbing up and down semi-naturally. The harder the barrel falls into the water, the deeper the barrel goes, but then pops up out of the water harder.

Is this clearer?

Thanks,
Shawn
#4
02/12/2007 (4:31 pm)
@Shawn, with the new info ... I've got other ideas ... heh, go figure, eh? :)


If you have a side-scroller, and your water takes up say ... some portion of the bottom of the screen ... you could create a 'water' trigger ... 'onEnter' will be called when an object enters the trigger (touches the top of the water) at which point, the physics of the object can be checked (weight, velocity) then upward force can be applied to the object for a set period of time, based on these physics ... then, you would set some sort of timer, and basically apply upward/downward force on the object to simulate the 'bob up and down on the surface of the water' ... and in that same timer, play with the rotation of the object ...

You could also do this with an Animated Sprite, as I mentioned for the top-down view ... the only thing you'd need to do at that point is apply the upward force on the object when it enters the trigger, set a timer to stop the force from being applied, set the object in a static location, then start a 'bob' animation that shows the object bobbing up/down/left/right on the water surface ...

The upward force, when applied, would not immediately 'take effect' as the object is already in a downward motion ... think of it like reverse gravity basically ... but you only apply this 'reverse gravity' once it enters the water (hits the surface, 'onEnter' trigger is called) -- you would then know, based on the 'weight' and 'speed' of the object, how long to set the timer for --

Now ... I say "you'll know based on weight and speed" ... and what you should be reading out of that is ... "you've got lots of timing and testing before you get it right" ... ;)
#5
02/12/2007 (4:36 pm)
@Shawn - there are a number of ways you could do this. One way would be to set up a T2DTrigger object that covers your water area. Set the "onEnter" callback to put a constant force upwards on your object, and the "onLeave" callback to get rid of the constant force. Assuming you have something else taking care of gravity in the scene, I think this would probably give you your bobbing effect.
#6
02/12/2007 (4:58 pm)
@Dan -- heh, beat you to it ... *chuckles* -- not a race, just thought that was funny :)
#7
02/12/2007 (7:36 pm)
I've set up a trigger, but I can't seem to get anything working. I've got onEnterCallback and onLeaveCallback defined, but when my object (that I've created dynamically) intersects, neither function gets called. I'm sure I'm missing something really simple. Do I have to setup physics or collisions in my trigger? Collision layers/groups?

Thanks!
Shawn
#8
02/12/2007 (7:38 pm)
@Shawn, there's a couple of trigger usage examples in the docs/tdn and the forums -- search around for them, I'm not all that familiar with there usage off the top of my head ...

If the docs don't help, let me know ...
#9
02/12/2007 (8:22 pm)
@Shawn - You need to enable collisions on your object to get the trigger callbacks to fire. From doing some experiments in the editor, it looks like either send or receive collisions on the object will make it work.
#10
02/12/2007 (8:34 pm)
So I just create the trigger, set its class to my own (waterClass), override onEnterCallback() and onLeaveCallback(), then set any other object's collisions to send or receive and it should just work? It's doesn't seem to work on my side. Crap.

Oh, seemingly unrelated question: would it change anything if I'm running the mac version?
#11
02/12/2007 (8:44 pm)
That's mostly right but the names of the functions you want to override are just "onEnter" and "onLeave". Running on a Mac shouldn't matter.
#12
02/13/2007 (5:00 am)
Ah! So that's what it was! :)

Thanks!
Shawn
#13
02/13/2007 (6:23 am)
Dan, heh, to be perfectly honest, running on a mace would make a big difference ... ;)
#14
02/14/2007 (7:04 am)
So I've been messing around with water idea and it seems to work okay, but not exactly what I'd like. (right now my water trigger sets the intersecting object's constant force to -gravity) There are a whole bunch of additional physics settings that I can play with, but I'm not sure which ones will be useful for me. Settings like inertia, density, friction, and restitution seem more related to when two objects are hitting each other rather than intersecting. Would any of them be applicable to my case?

Also, is using Constant Force a good idea when I want to use forces in addition to gravity? If I want to apply another force (like wind, for example) to an object that's falling, do I have to manually compute all the forces I want to apply to the object? Like:

final force = gravity force + wind
%object.setConstantForce(final force)

?

Thanks!
Shawn
#15
02/14/2007 (8:09 am)
@Shawn, constant forces are 'constantly' applied -- and all applied forces are taken into consideration when altering an objects movement --

Restitution is the speed in which the object comes to a rest, so if you put the object into motion using a force, the object will come to a rest after a period of time due to its restitution -- weight also dictates how much force it takes to move the object, and is also taken into consideration for the objects friction -- there is no concept of 'space' in TGB, so friction and weight are constantly applied, from what I can tell ... just picture all your objects laying on a flat table, and the friction is applied to that table surface as they move ... the weight takes into account the objects inertial moment ... which is ... if the object is 100lbs, it takes 150lbs of force to get the object in motion (the 150lbs is the moment of interia or whatever) but you can then keep the object in motion with less force ...

For example, a car parked in a parking lot requires quite a push to get it rolling... but once it's rolling, you can lay off the massive force and just apply a steady smaller force to keep it rolling ...

There are tons of physics examples, explanations and other such goodies on the web, google and you'll find tons of them -- Billiards seems to be the defacto standard example, which is great, since they usually explain it in a 2d concept... which is what your looking for ...

Granted, I could be mistaken in some of my explanations, I read up on the subject a bit when i first got into TGB, and when I realized that TGBs physics reactions (rigid collisions, heh) were a bit ... 'off' ... I shortly gave up on it ... I was originally going to make a billiards game in TGB ... and it failed to react properly to most collisions ... without making C++ changes, or writing a lot of code to compensate, I figured it would be too much work for a 'starter' project ... though, I did have a fairly fun prototype of a billiard table with balls and a cue stick to hit them with ... no game play ... and frustrating reactions and odd-ball 'jump behind another object' goofs ...
#16
02/14/2007 (8:20 am)
@Shawn - Yes, in TGB you'll need to manually add up the forces (although I think there may be a way to set one at the scenegraph level to apply to every object in the scene, my memory with that was that it was more trouble than it was worth for me).

Most of the physics parameters are indeed collision related, but there may be one or two that aren't. Without knowing what feels wrong about your game it's hard to make suggestions (although you have my sympathies if "you can't put your finger on it", since this happens a lot when you're trying to tune game physics to be cool and fun). My first guess is that it will seem more interesting if you use different values for the gravity and the upward buoyant force. Making them identical will probably result in too regular a motion. But it's hard to guess without seeing what you're seeing.
#17
02/14/2007 (8:26 am)
@Shawn -- if it helps, I usually make things like forces, etc, based off of global vars ... then I either create a GUI to alter these variables, or I change them in the console window ... this way I can 'nudge' the forces around until they seem 'just right' ... like for example, adding a slider to the mainscreengui ... then applying the slider value to the $GRAVITY_FORCE variable ... now I can just move the slider back and forth, and the gravity changes each time i 'test play' ... sometimes, I'll write a loop that just repeats the scene over and over again, like an animation ... get the values 'just right', then remember them, and code them into the game ...the global scope thing is usually useful in a 'test' project ...

copy over the level, write some 'test' script code ... and keep going, till its 'just right' ...

For something like the bobbing, I would think that a reverse gravity and an animated sprite that bobs the barrel left/right would do just right ... if your trying to apply the left/right movement with forces, your going to have quite a lot of work to do ... as you'd have to constantly change the 'wind' or 'wave' force from positive to negative back to positive, over and over ... in some form of a timed fashion.