Reputation: 10330
So I'm trying to learn some 3D programming and am trying to do some experiments to better understand how things work.
One thing I'm trying is (with threejs) drawing a line and rotating it randomly.
I've never been a matrix guy, I always did things in 2D the way it "made sense in my head"
# CoffeeScript
get_angle:->
Math.atan2(@props.velocity[1],@props.velocity[0])
rotate:(amount)->
ang = @get_angle()
mag = @get_speed()
# props is a velocity vector, [0] is x and [1] is y
@props.velocity[0] = Math.cos(ang+amount)*mag
@props.velocity[1] = Math.sin(ang+amount)*mag
The amount passed in is random... Generating a random "walk"
I was wondering now how I could do the same in 3d? Right now the application runs fine (and I see my line), but it's "flat." There is no depth (due to the Z component not being modified) This is what I was thinking of doing, would this be a smart way of doing this?
get_angle:->
Math.atan2(@props.velocity[1],@props.velocity[0])
get_angle2:->
# not sure about this, but I feel like I need to be finding the angle from the z now and something else.
rotate:(amount)->
ang = @get_angle()
ang2 = @get_angle2()
mag = @get_speed()
# props is a velocity vector, [0] is x and [1] is y
@props.velocity[0] = Math.cos(ang+amount)*mag
@props.velocity[1] = Math.sin(ang+amount)*mag
@props.velocity[2] = # I have a feeling here I would do something with ang2 and amount?
I know that technically I would probably want to pass in the rotation for two of the components, but I thought maybe I could just try having it rotate the same for all three components.
Upvotes: 0
Views: 1290
Reputation: 16505
Rotation in 3d a bit more complicated than in 2d because instead of a point (2d) you have to rotate around a line, or use a quaternion. Look here to see the different combinations of rotation matrices depending on the line you rotate around. 3d transformation involve four by four matrices to create homogeneous coordinates and the three by three part of the matrix is needed to create rotations.
You can also use quaternions (special kind of Number) to calculate Rotations, what is quite convenient to use in 3d applications because it solves some issues concerning to »gimbal lock«.
Upvotes: 1