- Home /
Issues with Sine Wave Generation and Rotation
I'm working on generating objects in sine wave that lets you specify parameters that are start x, end x, start y, and end y position.
Below is the method that I used to generate the debris
void generateWaveDebris(float startX, float endX, float startY, float endY, int numberToGenerate, bool randomizeDebrisDirection = false, bool randomizeDebrisFallSpeed = false, int[] numbersToSkip = null) {
float totalXDistance = endX - startX;
float xPositionOffset = 0f;
float xStepSizeForGeneration = totalXDistance/numberToGenerate;
xPositionOffset += xStepSizeForGeneration/2;
float totalYDistance = endY - startY;
float yPositionOffset = 0f;
float yStepSizeForGeneration = totalYDistance/numberToGenerate;
yPositionOffset += yStepSizeForGeneration/2;
int generatedDebrisTracker = 0;
float debrisRotation = 0;
float debrisRotationSpeed = Random.Range(debrisImageRotationSpeedMin, debrisImageRotationSpeedMax);
float debrisFallSpeed = Random.Range(debrisFallSpeedMin, debrisFallSpeedMax);
bool skipThisDebris = false;
Vector3 newPosition = Vector3.zero;
newPosition.z = defaultGeneratedDebrisZPosition;
while(generatedDebrisTracker < numberToGenerate) {
if(numbersToSkip != null) {
foreach(int number in numbersToSkip) {
if(generatedDebrisTracker == number) {
skipThisDebris = true;
}
}
}
// X Tracking ----------------
newPosition.x = startX + xPositionOffset;
xPositionOffset += xStepSizeForGeneration;
//----------------
// Y Tracking ----------------
newPosition.y = startY + yPositionOffset;
yPositionOffset += yStepSizeForGeneration;
//----------------
float tempX = newPosition.x;
float tempY = newPosition.y;
newPosition.x += Mathf.Sin(normalizeValue(tempY, startY, endY, 0f, 359.9f)) * 5;
newPosition.y += Mathf.Cos(normalizeValue(tempX, startX, endX, 0f, 359.9f)) * 5;
if(!skipThisDebris) {
GameObject newDebris = null;
newDebris = generateRandomDebris(newPosition);
if(newDebris != null) {
Debris newDebrisReference;
newDebrisReference = newDebris.GetComponent<Debris>();
newDebrisReference.defaultObjectZRotation = 0;
//This will randomize the direction that the debris falls in if
//it is set to true when the method is called
if(randomizeDebrisDirection) {
newDebrisReference.defaultObjectZRotation = Random.Range(debrisRotationMin, debrisRotationMax);
}
else {
newDebrisReference.defaultObjectZRotation = debrisRotation;
}
//This will randomize the fall speed of the debris if
//it is set to true when the method is called
if(randomizeDebrisFallSpeed) {
newDebrisReference.fallSpeed = Random.Range(debrisFallSpeedMin, debrisFallSpeedMax);
}
else {
newDebrisReference.fallSpeed = debrisFallSpeed;
}
newDebrisReference.rotationSpeed = debrisRotationSpeed;
newDebris.transform.parent = transform;
}
}
//-----------------------------
generatedDebrisTracker += 1;
//Resets for next loop
skipThisDebris = false;
}
}
It's important to note that the sections that matter are the lines:
newPosition.x += Mathf.Sin(normalizeValue(tempY, startY, endY, 0f, 359.9f)) * 5;
newPosition.y += Mathf.Cos(normalizeValue(tempX, startX, endX, 0f, 359.9f)) * 5;
When the line's start x and end x are the same, the wave generates fine. However when the line is diagonal it gets all sorts of messed up.
I know that there are ways to generate a sin wave and then use a linear transformation matrix to rotate it, however I've had a ton of issues getting that going, and it doesn't resolve one of my requirements where I specify the start x, end x, start y, and end y. This is because when the linear transformation rotation matrix is applied it doesn't keep the line scaled to the points I want.
So my question is: Is there a way to make a sine wave that can be oriented in any rotation, as well as specifying its start and end points for the x and y values?
If there is any more information I can provide, or anything I can do to help facilitate answers I would be more than happy to provide it.
As this isn't specifically Unity - you might try StackOverflow.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Define circle using 3 points 0 Answers
How to let the user draw lines in a 2D game? 1 Answer
Creating the score in hidden object game 1 Answer
Vector Math Question 2 Answers