Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hugbot · Apr 28, 2013 at 10:39 PM · 2d gamemathgeometry

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Apr 28, 2013 at 10:40 PM 0
Share

As this isn't specifically Unity - you might try StackOverflow.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges