Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Asteroids! Toroidal Topology with expressions?

  • Asteroids! Toroidal Topology with expressions?

    Posted by Jason Guest on February 15, 2011 at 1:33 pm

    Hello there,

    I’m looking to do something with expressions that I thought would be rather easier. What I want to recreate is the look of the old “Asteroids” game, which uses “toroidal topology” – a fancy way of saying that when objects reach the top of the screen, they re-appear at the bottom. The same goes for left-right, right-left and bottom-top.

    I have this really simple bit of positioning script which allows causes layers to move continuously with the help of a couple of sliders:


    v = effect("Slider Control x")("Slider");
    w = effect("Slider Control y")("Slider");

    f = timeToFrames();
    f0 = timeToFrames(inPoint) + 1;
    myValx = 0;
    myValy = 0;

    for ( i = f0 ; i <= f ; i ++){
    t = framesToTime(i);
    myValx += v.valueAtTime(t);
    myValy += w.valueAtTime(t);
    }

    value + [myValx,myValy]

    I haven’t set any keyframes for the sliders because I’m happy for the asteroids to move at a constant rate. I can just duplicate the asteroids, tweak the sliders for each and off they go. The problem is that because the code doesn’t actually change the value of position[0] and position[1] I can’t tell it to wrap-around the position when it reaches edge of frame:

    if (position[0] >= 720){position[0] == 10;}
    if (position[0] <= 0){position[0] == 710;} ……………….etc, won’t work.

    What I need is something that takes the value of x, plus or minus the value of myValx depending on which direction it’s going, subtracts this from the distance to the edge of the screen and then adjusts myValx accordingly to move it to the other side of the screen. It is at this point that my head starts to go around and around in circles like that of Colonel Dedshott when Professor Branestawm starts explaining one of his inventions.

    Can anyone help me with this, or suggest a better way of doing it?

    I would be most awfully grateful.

    Jason Guest

    Dan Ebberts
    replied 15 years, 2 months ago
    2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    February 15, 2011 at 5:19 pm

    Assuming that you’re not keyframing the sliders, something like this should work:

    v = effect(“Slider Control x”)(“Slider”);
    w = effect(“Slider Control y”)(“Slider”);

    t = time – inPoint;
    p = value + [v,w]*t;
    x = (value[0] + v*t)%thisComp.width;
    if (x < 0) x += thisComp.width;
    y = (value[1] + w*t)%thisComp.height;
    if (y < 0) y += thisComp.height;
    [x,y]

    You’ll have to make some adjustments if you want to restrict the area to something other than the dimensions of the comp.

    Dan

  • Jason Guest

    February 15, 2011 at 5:28 pm

    That’s perfect, and a lot less complicated than I expected!

    Thanks Dan.

  • Dan Ebberts

    February 15, 2011 at 5:36 pm

    I’m glad it works for you. I just noticed a line in the the expression that doesn’t need to be there:

    p = value + [v,w]*t;

    It doesn’t hurt anything, but it doesn’t serve any purpose.

    Dan

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy