Game Development Community

Programming Suggestions

by USC - IMD student 3 · in Torque Game Builder · 02/20/2006 (2:41 am) · 14 replies

Hello,
I'm pretty new to Torque 2D, and I've been trying to figure out a number of things.
I have been trying to simulate something similar to a molecule with orbiting electrons.
For my code I haven't figured out how to utilize gravity towards objects, as opposed to constant force in a certain direction. To compensate for this I've used the code:

%v = VectorSub($molecule.getPosition(), $electron::names[%i].getPosition());
%v = VectorNormalize(%v);
%x = getWord(%v, 0) * %fg;
%y = getWord(%v, 1) * %fg;
$bubbles::names[%i].setConstantForce(%x SPC %y SPC "0");

and I've created a formula to determine %fg out of the distance between the molecule and the electrons.
i call this recursively every 10ms (using schedule), in order to keep the electrons on the path.

there are a couple problems.
1. the way the electrons orbit are very unnatural, and don't look that great. the way that it looks is really bad, and when the electron reaches the tip of its orbit, it takes waaay too long to start heading back to the molecule.
2. when i have multiple electrons, i need a for-loop in my function which is constantly changing ALL the electrons forces, so every 10ms it is running a for loop for all the electrons, (sometimes i need 20!)
3. torque EATS up my memory...LIKE CRAZY! I feel like there is some sort of garbage build up in my program, and I can't figure out how to reduce my code.

So basically, does anyone have suggestions to do this? Are there gravity functions so i don't have to do what I'm doing? Can I have more than 20 objects constanly moving without freezing up my computer?
And lastly, can I do this without a recursive schedule() call?

Thanks so much

#1
02/20/2006 (4:05 am)
Is it important that the electrons be their own objects, or can you make it an animation?

Half your problem is probably that your force isnt correct. The force of gravity is different for each set of orbiting bodies. You can't use 9.8 in a calculation that isn't for gravity on the earth.

Also, youre going to find that all your electrons move either infront or behind the nucleus, and don't alternate. If this is purely for looks I would suggest creating an animation or a 3D object and using it as a t2d3dshapes object.

You're not running your code every 10ms. At most it will run every 32 ms, as that's the length of an engine tick, so you should change that in your schedule.
#2
02/20/2006 (4:47 am)
I wasn't using 9.8 in my calculations.
i was using fg = Gmm/r^2
I simplified it down like i said, to determine %fg.
each electron does need to be it's own unit.
anyway, I've determined how to do what i needed to do.
the main problem now is the memory usage. I'm looking at the amount of memory that my 2td program uses, and it CONSTANTLY grows, without ceasing, regardless if i create new electrons or not, and regardless if it's 10ms or 32ms in which i schedule my function to determine the direction and force of each electron.
#3
02/20/2006 (5:05 am)
It's be nice if you share your solution so anyone who has the same problem in the future can figure out how to do it also.

Are you creating images anywhere and not safedeleting them ?
#4
02/20/2006 (5:48 am)
Because the objects I've created don't have mass, instead i've made a simple run around to determine %fg.
I basically did %constant / (%distance * %distance). i tweaked the numbers and finally after A LOT of trial and error I found the optimal solution and settings for what I wanted. I also created a formula to set the damping of each of the objects so they don't get TOO far away from my molecule. so in order to do this, i did, %electron.setDamping(%distance * %distance / %dampingConstant). It's kinda bad looking, but I finally got it to look decent.

As for the safedeleting of images...
What my program currently does, is starts off with a molecule. I press a button, and an electron is added. The forces and damping and everything are calculated every schedule..(whether 10 or 32 or whatever). If i add more electrons they are added in the function that is scheduled. The thing is, I don't want to delete my objects! I want to have a BUNCH of electrons orbiting my molecule without any slowing of processing.
It's funny, because my computer can run Half life 2 and doom 3 really well, but it slows down a great deal when i use my torque game with more than 10 objects! And the thing is, that the memory continually gets sucked up! It's not that it's just using a lot at once, but it starts off at a reasonable level, then the longer I leave my program running, the more memory it takes up!
I think it's because of all the schedules I'm running, but I'm not sure.

Thanks again
#5
02/20/2006 (6:19 am)
If your memory usage continuously ups itself you must be creating something every schedule and not deleting it properly.

I created a program that was running a scheudle every 100 ms on multiple objects and it destroyed my performace.

You might have a simlair problem.
#6
02/20/2006 (12:43 pm)
A thought, but you aren't spawning additional schedules are you? Meaning that for each electron there should only be one schedule at any given time.

If you have numerous objects on the screen, each with damping and its own 32ms scheduled force you *will* have performance issues. I wrote 'bobbles' a while back which used two forces (strong and weak) varied over distance, with size and by color interaction. That is, each bobble interacted with other bobbles and potentially with every other bobble on the screen. I don't remember the threshold anymore, but after getting a enough bobbles on the screen it would start to hitch intermittently.

Incidentally, I implemented this via collisions rather than scheduled applications of force. This makes it smooth with the engine and, in my case, allowed me to set an outer bound past which the force could be neglected which helped with performance.

Tim Doty
#7
02/20/2006 (6:10 pm)
How do you do this with just collisions? From what I know, I'm not creating any new objects each time I schedule, and it SHOULD theoretically be only doing 1 instance of the schedule each time....unless it takes more than 32ms to complete my entire function. Which MIGHT be the case when I get to a large number of electrons.

Do you have that bobble's code available for everyone to look at, or is it personal?

Thanks
#8
02/20/2006 (6:18 pm)
Quote:How do you do this with just collisions?
You override the onCollision() function. It has been quite a while since I did this so my memory is a little fuzzy. That gives you pointers to each object and you can then call appropriate functions to set the force. I use impulse forces for this. Thinking of which, are you using impulse forces or constant?

Quote:unless it takes more than 32ms to complete my entire function
I'm really not sure why you're using such a short time interval. In another (shelved currently) project I'm pretty sure I used 100ms schedules for the homing missiles and that worked quite well.

Quote:Do you have that bobble's code available for everyone to look at, or is it personal?
It's personal, but I can try and pull out some code. Note that this was pre-Alpha and naming conventions have changed and such. If I get time I may update it for the new versions, but I've got a serious project on my plate right now.
#9
02/20/2006 (6:23 pm)
Okay, I don't know if this is enough to be intelligible, but here's some snippets. The first one is just an override of the onCollision function. My real function has a lot more stuff in it dealing with actual bobble collisions with color interaction, but I don't think the whole function would contribute anything.
function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) {
  if (%srcObj.force !$= "" || %dstObj.force !$= "") {
    ForceCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts);
    return;
    }
  }

Again, this is only part of a function just to illustrate how I did things.
//-----------------------------------------------------------------------------
// This subfunction handles the details of force-force or force-bobble
// collisions.
//-----------------------------------------------------------------------------

function ForceCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) {
  %normal = vectorNormalise2D(vectorSub2D(%srcObj.getPosition(), %dstObj.getPosition()));
  if (strCmp(%srcObj.force, "strong") == 0) {
    // get the bobble
    if (strCmp(%dstObj.force, "") != 0) %bobble = %dstObj.bobble;
    else %bobble = %dstObj;
    if (%bobble != %srcObj.bobble) {
      // establish the force strength
      if (%bobble.color == %srcObj.bobble.color) %force = $StrongForce0 * $ForceMult;
      else if ((%bobble.color + %srcObj.bobble.color) % 2 == 0) %force = $StrongForce1 * $ForceMult;
      else if (mAbs(%bobble.color - %srcObj.bobble.color) == 3) %force = $StrongForce2 * $ForceMult;
      else %force = $StrongForce3 * $ForceMult;
      
      %bobble.setImpulseForce(vectorScale2D(%normal, %force * %srcObj.bobble.size), true);
      }
    }
  }

Edit: I notice there are some inconsistencies in the code. Like using strcmp() and $=. It also uses the old fx naming scheme. I'm not saying this is exemplary code...
#10
02/21/2006 (1:59 am)
Thanks for the quick replies, both of you.
So previously I had chosen 32ms because prior my calculations were in correct and I wasn't getting the output I wanted and 10ms seemed to fix it.
I changed it to 100ms and it seems to run decently.
while it doesn't nearly take up as much memory as it used to, it still is constantly increase memory usage.
my pseudo code is as follows:
function update()
{
    for (each electron)
        check distance
        calculate force using distance
        subtract v1 - v2
        normalize
        set constant force to fg * normalized vector
        setdamping dependant on velocity
     loop
     schedule update in 100ms
}
any reasons why it would continually eat up system resources?
#11
02/21/2006 (4:09 am)
If you're wanting to create gravity wells that suck objects towards them, then although you can manage this in script via schedules and adjustments to the constant force there is an alternative. Take a look at a resource Melv posted public.garagegames.com/melvm/unsupported/force.zip. More info on this can be found in this thread www.garagegames.com/mg/forums/result.thread.php?qt=28261

Be warned though that its a totally unsupported resource and probably won't be plug and play with the latest version of T2D. But if you don't mind getting your hands a little dirty it may provide the best results rather than trying to script it.
#12
02/21/2006 (5:09 am)
The eating up resources is most likely your using the constant force. Try using an impulse force instead. The constant force is really meant for something that doesn't change vector or magnitude and here you are constantly changing both.

Also, something I realized after I put up my post was that it may not have been clear that the "forces" I use are sceneobjects mounted to the visible objects and sized appropriately. I used polygon collision shapes, but in the new version you can specify circles which will improve on boundary situations.

In sum:

1. The forces in bobbles were represented by sceneobjects mounted to the "real" objects
2. Try using impulse forces rather than constant forces.
#13
02/21/2006 (4:30 pm)
Hmm...set impulse force seems to be a bit too jaggy.
When i use constant force it's a lot smoother.
However, I haven't had a chance to tweak the numbers, so maybe i can get it to look a lot smoother if i mess with them.

thanks
#14
02/21/2006 (5:41 pm)
No problem. I hope you get it working to your satisfaction.