- Home /
How do I make a rotatable procedural circle in 3d space?
I'm trying to make a plant that grows upward in different variations depending on variable changes within it's script(s). How I've been running it so far is with a game object that starts at point zero, makes 8 vertices in a circle around it, which would be the base of the plant. Next, I would like to move the gameObject upward and rotate it some, then spawn 8 more vertices that will get connected to the first 8 to create the beginnings of a stem.
The problem I'm having, is that I can't get the vertices to spawn in at a rotated angle along with the gameObject...they're flat, on the gameObject origin with no rotation. I believe my problem is that my vertices aren't being created on the gameObject's x,y,z but world relative. How do I rotate the game object and then manifest these points onto th game object's axis?
void Start ()
{
all of these points are Transforms
point1 = getPoint(point1,360);
point2 = getPoint(point2, 45);
point3 = getPoint(point3, 90);
point4 = getPoint(point4, 135);
point5 = getPoint(point5, 180);
point6 = getPoint(point6, 225);
point7 = getPoint(point7, 270);
point8 = getPoint(point8, 315);
}
Vector3 getPoint(Vector3 changePoint, int angle)
{
float myCos = position.x +Mathf.Cos( angle * Mathf.Deg2Rad)*radius;
float mySin = position.z +Mathf.Sin(angle * Mathf.Deg2Rad)*radius;
changePoint.position= new Vector3(myCos,0 , mySin);
return changePoint;
}
What's Happening: I'm growing a plant. The PlantHead comes from the seed and releases a game object. The game object has 8 vertices that it creates around it. The PlantHead moves on and keeps dropping these game objects. A separate script pieces them together as a mesh and the game objects control the vertices as the plant is growing.
**The Problem: **When I rotate the PlantHead and then it drops a game object, the game object is in the proper rotation, but the vertices are still level...unrotated.
The code above is what I've currently got as the vertex plotter. I suspect the problem lies within the math...perhaps a quaternion issue? I've been searching and working on this for a day or so now.
Added a response to your new drawing to the bottom of my Answer.
Answer by robertbu · Mar 01, 2014 at 01:19 AM
I'm confused some about what you are really asking. Plus I'm not sure where you are going with the points you generate. Build a mesh? Build a plant out of primitive objects? Anyway here is a bit of code that creates rings of spheres. Each ring is rotated by 'offset' from the previous ring. 'divisions' determines how many spheres are in each ring. These rings are built relative to the game object. They start at the game object and are built in the 'up' of the object. In addition, they are made children of the object the script is on, so you can rotate all the spheres as a unit by rotating the parent game object.
To use, create a new scene, create an empty game object. Position the empty game object at (0,-1.5,0), add a directional light to the scene (Game Object > Create Other > Directional Light), add the following script, and hit play:
using UnityEngine;
using System.Collections;
public class Circles : MonoBehaviour {
private float offset = 3.0f;
private float segHeight = 0.35f;
private float radius = 1.25f;
private float divisions = 16;
void Start ()
{
float angle = 360.0f / divisions;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < divisions; j++) {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Vector3 pos = GetPoint(j * angle + offset * i, segHeight * i);
go.transform.position = transform.TransformPoint (pos);
go.transform.parent = transform;
}
}
}
Vector3 GetPoint(float angle, float z)
{
float myCos = Mathf.Cos( angle * Mathf.Deg2Rad)*radius;
float mySin = Mathf.Sin(angle * Mathf.Deg2Rad)*radius;
return new Vector3(myCos,z , mySin);
}
}
Ahhh now I understand your question given your new drawing. The fix is pretty simple. You need to access the transform of your cube/seed. Given that you are adding the position to your calculation, I'm going to assume that this script is on the object that is rotated. The approach is to generate local coordinates then use Transform.TransformPoint() to convert them into world positions. This takes into account, position, rotation, and scale of the object that has the transform. You may have to increase your radius if your 'cube/seed' object is not scaled to (1,1,1), and if you change the scale of your 'cube/seed' object dynamically, you will have to modify the 'radius' dynamically. So here is the change to your code:
Vector3 GetPoint(float angle)
{
float myCos = $$anonymous$$athf.Cos(angle * $$anonymous$$athf.Deg2Rad) * radius;
float mySin = $$anonymous$$athf.Sin(angle * $$anonymous$$athf.Deg2Rad) * radius;
Vector3 v = new Vector3(myCos, 0.0f, mySin);
return transform.TransformPoint(v);
}
As an alternate you could use just rotation and position of the object. Another version (untested) where you don't have to deal with scale:
Vector3 GetPoint(float angle)
{
float myCos = $$anonymous$$athf.Cos(angle * $$anonymous$$athf.Deg2Rad) * radius;
float mySin = $$anonymous$$athf.Sin(angle * $$anonymous$$athf.Deg2Rad) * radius;
Vector3 v = new Vector3(myCos, 0.0f, mySin);
v = transform.rotation * v;
return transform.position + v;
}
Dude! Thank you! That's all it was! I've been going crazy trying to figure this out. Thank you so much. Now that this is figured out, I should have a model of this thing up and running soon. I'll send you a link to it in here. I tried to hit the thumbs up over there on the side but it says I don't have a large enough reputation to tell you good job and thank you through a green thumb. Thank you so much for your help.
Rather than thumbs up, when your questions is answered, click on the checkmark. This closes the question out and you will see the icon on the question list as green.
Your answer
Follow this Question
Related Questions
[Solved] Issues mapping procedurally generated cube without repeating vertices 0 Answers
Holes in procedural mesh 0 Answers
Performance increase by unused Vertices? 1 Answer
How can i draw a circle pie? 2 Answers
Set vertex positions aligned to a grid 2 Answers