# Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects PONG expression

• # PONG expression

2 Members · 2 Posts
• ### Jalal Jemison

September 21, 2005 at 12:09 am

I got this script from further down the forum.
I am trying to make a script that is like pong. Where the ball bounces between the four sides of a box, forever. I can add the paddles after, to make it look like someone is playing the game.

I got this expression off creativecow.net but it doesn’t bounce between the sides

box=[10,10,100,200]; // box you want to stay in [x1,y1,x2,y2]
motion=[312,123]; // motion-vector in pixels per sond [x,y]
startpos=[160,120]; // startposition

// calculations:
w=box[2]-box[0]; h=box[3]-box[1]; corner=[box[0],box[1]];
delta=motion*time; pos=startpos+delta-corner;
xx=pos[0]%(w*2); yy=pos[1]%(h*2);

corner+[xx,yy]

Anyone got a script that would bounce an between the four sides of the screen forever?

Thanks

• ### Dan Ebberts

September 21, 2005 at 2:31 am

Try this one:

left = 0;
top = 0;
right = thisComp.width;
bottom = thisComp.height;

minSpeed = 200; //pixels per second
maxSpeed = 500;

minX = left + 20;
maxX = right – 20;
minY = top + 20;
maxY = bottom – 20;

minR = 5;
maxR = 85;

seedRandom(index,true);

// calc start position

pos = random([minX,minY],[maxX,maxY])
spd = random(minSpeed,maxSpeed);

// initialize time count;

t = 0;

while(t <= time){ rads = degreesToRadians(angle); xVel = spd*Math.cos(rads); yVel = spd*Math.sin(rads); // see if going up or down if (yVel < 0) yLim = top else yLim = bottom; // see if going right or left if (xVel < 0) xLim = left else xLim = right; timeX = (xLim - pos[0])/xVel; timeY = (yLim - pos[1])/yVel; if (timeX < timeY){ //hit left or right wall if (t + timeX >= time){
deltaT = time – t;
pos += [xVel,yVel]*deltaT;
break;
}else{
pos = [xLim,pos[1] + yVel*timeX];
angle = 180 – angle;
t += timeX;
}
}else{ //hit upper or lower wall
if (t + timeY >= time){
deltaT = time – t;
pos += [xVel,yVel]*deltaT;
break;
}else{
pos = [pos[0] + xVel*timeY,yLim];
angle = 360 – angle;
t += timeY;
}
}
}
pos

Dan

Viewing 1 - 2 of 2 posts