- Home /
Rotating an object in a 3D grid
I have a system working for building a custom object out of blocks in a 3D grid environment. I manually set the size of each piece in a Vector3 and then add it like so:
public void AddToGrid (Vector3 gridPos, GunPart _part) {
//adds a reference to an object for every space it occupies on the grid
int x = (int)gridPos.x;
int y = (int)gridPos.y;
int z = (int)gridPos.z;
for(int sx = 0; sx < _part.size.x; sx++){
for(int sy = 0; sy > -_part.size.y; sy--){
for(int sz = 0; sz < _part.size.z; sz++){
//adding the part at XYZ
gunGrid[sx + x, sy + y, sz + z] = _part.gameObject;
}
}
}
}
If I want to be able to rotate the pieces, I'm not sure how to take the euler angles and apply it to that size to accurately shift the X,Y and Z of the size. The origin of each piece is in the top/left/back so I'd also need to know where it's orienting and how to move along the grid accordingly. I've tried to google this but found nothing.
Answer by Benproductions1 · Sep 15, 2013 at 11:18 PM
Hello,
This is the main reason why Quaternion
s are amazing!
If you create a quaternion rotation:
var Q:Quaternion = Quaternion.Euler(0, 90, 0);
You can rotate a Vector3
around the origin (`Vector3.zero`):
var V:Vector3 = Q*vector;
It really is that simple.
var V:Vector3 = Quaternion.Euler(0, 180, 0)*Vector3.forward;
//V will equal Vector3.backward
It''s leave the "apply to situation" to you :)
Hope this helps,
Benproductions1
This randomly makes the numbers slightly off and negative but those are easy to fix with $$anonymous$$athf functions. Other than that, thanks! The only thing I'm struggling with now is since the origin is no longer in a fixed place, how would I fill or remove from the grid? Not sure how to adjust the nested loop to it.
Edit: I guess the rotations making the size negative could actually be useful for this!
@dustingunn There is a accuracy trade off when using any rotation values. There is no need to round off the values because that error is so $$anonymous$$ute it's unnoticeable. I don't understand what you mean by "It makes the numbers negative". It allows you to rotate a Vector3
around (0, 0, 0)
. The angle is irrelevant and the values are correct :)
I do have to round it because I'm working with integers and 0.99999 would still equal 0. Array positions can't be float.
lol, I was assu$$anonymous$$g you were using Vector3
's for positions (like you should be). Its no reason you should be making 3 more variables
Like I just said, array positions can't be a float. I have no idea what you're talking about. I'm not positioning anything in the world, I'm storing objects in a 3D grid.
Answer by JamieSinn · Sep 15, 2013 at 10:32 PM
Usually you are going to want to use float's for positions, but it may not be as useful if there is just a grid. Have you looked into using _part.gameObject.transform.Rotate() ?
float's for positions... What the hell are you doing? A one dimensional game? Positions are either a Vector2 (2D) or a Vector3 (3D) and in any game engine always should be some sort of Vector class.
@JamieSinn again how is that usefull? Just do somevector3.x
. Why go though the trouble of defining another variable that's really just going to take up space and confuse people reading your code.
Your answer
Follow this Question
Related Questions
Rotate object based on another rotation above a certain threshhold 1 Answer
gameobject stops moving correctly when rotating 1 Answer
Smooth rotation about global axis instead of local axis. 1 Answer
The object turn but the axis dont (SOLVED) 3 Answers
I need Some Info About Rotation And Pvot 0 Answers