//Code from euler2quat by Graham Wakefield static int quat_from_euler(lua_State *L) { t_jit_quat q1; t_jit_vec3 v1; /* test argument type */ if(! lua_istable(L, 1)) luaL_error(L, "first argument must be a table"); table_to_vec3(L, 1, &v1); { float c1, c2, c3, s1, s2, s3, tw, tx, ty, tz; c1 = jit_math_cos(v1.y * QUAT_DEG2RAD_BY2); c2 = jit_math_cos(v1.x * QUAT_DEG2RAD_BY2); c3 = jit_math_cos(v1.z * QUAT_DEG2RAD_BY2); s1 = jit_math_sin(v1.y * QUAT_DEG2RAD_BY2); s2 = jit_math_sin(v1.x * QUAT_DEG2RAD_BY2); s3 = jit_math_sin(v1.z * QUAT_DEG2RAD_BY2); // equiv quat_multiply(&Qy, &Qx, &Q1); // since many terms are zero tw = c1*c2; tx = c1*s2; ty = s1*c2; tz = - s1*s2; // equiv quat_multiply(&Q1, &Qz, &Q2); // since many terms are zero q1.x = tw*c3 - tz*s3; q1.y = tx*c3 + ty*s3; q1.z = ty*c3 - tx*s3; q1.w = tw*s3 + tz*c3; } quat_to_table(L, &q1); return 1; }
void Flock_resetBoids(t_jit_boids2d *flockPtr) { long i, j; double rndAngle; for (i = 0; i < flockPtr->number; i++) { // init everything to 0.0 flockPtr->boid[i].oldPos[x] = 0.0; flockPtr->boid[i].oldPos[y] = 0.0; //flockPtr->boid[i].oldPos[z] = 0.0; flockPtr->boid[i].newPos[x] = 0.0; flockPtr->boid[i].newPos[y] = 0.0; //flockPtr->boid[i].newPos[z] = 0.0; flockPtr->boid[i].oldDir[x] = 0.0; flockPtr->boid[i].oldDir[y] = 0.0; //flockPtr->boid[i].oldDir[z] = 0.0; flockPtr->boid[i].newDir[x] = 0.0; flockPtr->boid[i].newDir[y] = 0.0; //flockPtr->boid[i].newDir[z] = 0.0; flockPtr->boid[i].speed = 0.0; for(j=0; j<kMaxNeighbors;j++) { flockPtr->boid[i].neighbor[j] = 0; flockPtr->boid[i].neighborDistSqr[j] = 0.0; } } for (i = 0; i < flockPtr->number; i++) { // set the initial locations and velocities of the boids flockPtr->boid[i].newPos[x] = flockPtr->boid[i].oldPos[x] = RandomInt(flockPtr->flyrect[right],flockPtr->flyrect[left]); // set random location within flyrect flockPtr->boid[i].newPos[y] = flockPtr->boid[i].oldPos[y] = RandomInt(flockPtr->flyrect[bottom], flockPtr->flyrect[top]); //flockPtr->boid[i].newPos[z] = flockPtr->boid[i].oldPos[z] = RandomInt(flockPtr->flyrect[back], flockPtr->flyrect[front]); rndAngle = RandomInt(0, 360) * flockPtr->d2r; // set velocity from random angle flockPtr->boid[i].newDir[x] = jit_math_sin(rndAngle); flockPtr->boid[i].newDir[y] = jit_math_cos(rndAngle); //flockPtr->boid[i].newDir[z] = (jit_math_cos(rndAngle) + jit_math_sin(rndAngle)) * 0.5; flockPtr->boid[i].speed = (kMaxSpeed + kMinSpeed) * 0.5; } }
void max_jit_turtle_int(t_max_jit_turtle *x, long n) { t_atom a[16]; double tempangle; double temp_x, temp_y; long dest_x, dest_y; long curstack = x->curstack; x->command = n; // why do we do this? i can't remember... // check to see if the integer received matches the ASCII code for one of these commands. // if so, compute the appropriate QuickDraw response and pass it out the outlet as a Max message. switch(n) { case (35): // '#' - increase pen size x->pensize[curstack] = x->pensize[curstack]+1; jit_atom_setlong(&a[0],x->pensize[curstack]); jit_atom_setlong(&a[1],x->pensize[curstack]); outlet_anything(x->turtleout, gensym("pensize"), 2, a); break; case (33): // '!' - decrease pen size if(x->pensize[curstack]>1) x->pensize[curstack] = x->pensize[curstack]-1; jit_atom_setlong(&a[0],x->pensize[curstack]); jit_atom_setlong(&a[1],x->pensize[curstack]); outlet_anything(x->turtleout, gensym("pensize"), 2, a); break; case (70): // 'F' - move forward and draw if(x->stacknew) { x->stack_x[curstack] = x->origin[0]; x->stack_y[curstack] = x->origin[1]; x->stacknew=0; } temp_x = (double)x->scale*jit_math_cos(x->thisangle[curstack]); temp_y = (double)x->scale*jit_math_sin(x->thisangle[curstack]); dest_x = x->stack_x[curstack]+temp_x+0.5; dest_y = x->stack_y[curstack]+temp_y+0.5; jit_atom_setlong(&a[0],x->stack_x[curstack]); jit_atom_setlong(&a[1],x->stack_y[curstack]); jit_atom_setlong(&a[2],dest_x); jit_atom_setlong(&a[3],dest_y); outlet_anything(x->turtleout, gensym("linesegment"), 4, a); x->stack_x[curstack] = dest_x; x->stack_y[curstack] = dest_y; break; case (102): // 'f' - move forward and don't draw if(x->stacknew) { x->stack_x[curstack] = x->origin[0]; x->stack_y[curstack] = x->origin[1]; x->stacknew=0; } temp_x = (double)x->scale*jit_math_cos(x->thisangle[curstack]); temp_y = (double)x->scale*jit_math_sin(x->thisangle[curstack]); dest_x = x->stack_x[curstack]+temp_x+0.5; dest_y = x->stack_y[curstack]+temp_y+0.5; x->stack_x[curstack] = dest_x; x->stack_y[curstack] = dest_y; break; case (91): // '[' - start a branch if(x->curstack>(MAXSTACK-2)) { // you can uncomment this jit_object_post((t_object *)x,) if you prefer... it's kind of annoying, IMHO. // jit_object_post((t_object *)x,"out of stack range -- not branching"); } else { // copy current coords and angle to next branch and increment the stack x->stack_x[curstack+1] = x->stack_x[curstack]; x->stack_y[curstack+1] = x->stack_y[curstack]; x->thisangle[curstack+1] = x->thisangle[curstack]; x->curstack++; } break; case (93): // ']' - end a branch and decrement the stack if(curstack>0) x->curstack--; break; case (43): // '+' - turn right x->thisangle[curstack]+=((x->angle/360.)*PI2); break; case (45): // '-' - turn left x->thisangle[curstack]-=((x->angle/360.)*PI2); break; case (124): // '|' - turn around x->thisangle[curstack]+=(0.5*PI2); break; default: // no match, don't do anything break; } }