Pitch, Yaw, and Roll
BlitzMax Forums/BlitzMax Programming/Pitch, Yaw, and Roll
| ||
Multiple times in the past, I've tried to make a 3D engine for the heck of it because my 3D skills are nonexistent, and with every engine I make, my knowledge increases. Well, I've come quite far with this one but I'm puzzled on how to fix this bug I've been having with rotation.'pitch If x<>0 If v.z=0 If v.y>0 angle=90 Else angle=270 EndIf If v.z<>0 angle=ATan(v.y/v.z)+x dist=Distance2D(v.z,v.y,0,0) v.z=Cos(angle)*dist v.y=Sin(angle)*dist End If This is part of my function TurnCamera, and is the portion which handles pitch. X is the angle it needs to turn, and v.x, v.y, and v.z are the coordinates of each vertex. I'm having two problems. First, when using ATan(v.y/v.z), v.z is often zero, leaving me with NaN. That is why it does a check for if v.z is zero and determines the angle based on y if so. But the algorithm doesn't work anyway. What is wrong with my equation? |
| ||
I don't know for the "algorithm", but you should use Atan2 instead of Atan. This will remove the division by zero problem. |
| ||
Thanks much for your help, hopefully that solves the problem. Alright, rotation works quite nicely now, but I've run into another problem. My scaling function is not working as I intended for it to - Method Scale(x#,y#,z#) 'find center (average xyz of the vertecies) Local avgx#=0,avgy#=0,avgz#=0,scal% For scal%=1 To vertex_count avgx:+ver[scal].x-CamX avgy:+ver[scal].y-CamY avgz:+ver[scal].z-CamZ Next avgx:/Float(vertex_count) avgy:/Float(vertex_count) avgz:/Float(vertex_count) Print avgx+","+avgy+","+avgz 'scale it For scal%=1 To vertex_count ver[scal].x= (((ver[scal].x-avgx)*x)+avgx) +CamX ver[scal].y= (((ver[scal].x-avgy)*y)+avgy) +CamY ver[scal].z= (((ver[scal].x-avgz)*z)+avgz) +CamZ Next End Method |