后效果:在屏幕边框上反弹并以随机方向开始

问题描述 投票:0回答:1

我正在尝试重现多年前我们在电视上看到的 DVD 标志的运动。当标牌在屏幕上移动、接触边框、反弹等时..

我成功地完成了一些工作,虽然距离并不远,但有一个问题。具有此脚本的所有对象首先开始朝同一方向移动,即右下角。所以,根据他们的立场,过了一段时间,它就像我想要的那样工作,但是开始部分确实有问题,有人知道如何纠正吗?

// Seed random number generator to ensure consistent random values for start position and speed
seedRandom(index, true);

// Start position random 
startX = random(0, thisComp.width);
startY = random(0, thisComp.height);

// Speed random 
speed = random(50, 150); 

// Calculate distance moved since the beginning
distanceMovedX = time * speed + startX ;
distanceMovedY = time * speed + startY ;

// The effective width and height are reduced by the width and height of the layer
effectiveWidth = thisComp.width ;
effectiveHeight = thisComp.height ;

// Compute how many full "trips" across the screen (back and forth is 2 trips) for X and Y
numTripsX = Math.floor(distanceMovedX / effectiveWidth);
numTripsY = Math.floor(distanceMovedY / effectiveHeight);

// Calculate position based on the remainder of the distance after removing full trips for X and Y
positionInCurrentTripX = distanceMovedX % effectiveWidth;
positionInCurrentTripY = distanceMovedY % effectiveHeight;

// If the number of trips is odd, we are moving in the opposite direction; otherwise, we move in the initial direction
newX = (numTripsX % 2 === 0) ? positionInCurrentTripX : effectiveWidth - positionInCurrentTripX;
newY = (numTripsY % 2 === 0) ? positionInCurrentTripY : effectiveHeight - positionInCurrentTripY;

[newX, newY];
expression after-effects
1个回答
0
投票

你已经有了一个想法的开始,但它应该更像这样:

// Seed random number generator to ensure consistent random values for start position and speed
seedRandom(index, true);

// Start position random 
startX = random(0, thisComp.width);
startY = random(0, thisComp.height);

// Speed random 
speedX = random(-150, 150); 
speedY = random(-150, 150); 

// Calculate the new positions based on time and speed
newX = time * speedX + startX;
newY = time * speedX + startY;

// Check boundaries for X
if (Math.floor(newX/thisComp.width) % 2 == 0) { // even -> to the right
    newX = Math.abs(newX) % thisComp.width
}
else { // odd -> to the left 
    newX = thisComp.width - Math.abs(newX) % thisComp.width
}

// Check boundaries for Y
if (Math.floor(newY/thisComp.height) % 2 == 0) {
    newY = Math.abs(newY) % thisComp.height
}
else { 
    newY = thisComp.height - Math.abs(newY) % thisComp.height
}


[newX, newY];

这样,无论有多少次反弹,图像都将始终保持在屏幕边界内。

© www.soinside.com 2019 - 2024. All rights reserved.