Exemple #1
0
void rotate(snapshot *snap, string *tags, string *vecs, char axis,
	    real thetax, real thetay, real thetaz)
{
  matrix rmat;
  bodyptr bp;

  switch (tolower(axis)) {
    case 'x':
      xmatrix(rmat, thetax);
      break;
    case 'y':
      ymatrix(rmat, thetay);
      break;
    case 'z':
      zmatrix(rmat, thetaz);
      break;
    default:
      error("%s: unknown axis %c\n", getargv0(), axis);
  }
  if (set_member(tags, PosTag) && set_member(vecs, PosTag))
    for_all_bodies(bp, *snap)
      rotatevec(Pos(bp), rmat);
  if (set_member(tags, VelTag) && set_member(vecs, VelTag))
    for_all_bodies(bp, *snap)
      rotatevec(Vel(bp), rmat);
  if (set_member(tags, AccTag) && set_member(vecs, AccTag))
    for_all_bodies(bp, *snap)
      rotatevec(Acc(bp), rmat);
  if (set_member(tags, AuxVecTag) && set_member(vecs, AuxVecTag))
    for_all_bodies(bp, *snap)
      rotatevec(AuxVec(bp), rmat);
}
/// Rotate a 2d vector around (0, 0) by a specified angle.
// @function rotate2d
// @tparam number pos_x the x coordinate of the vector to be rotated
// @tparam number pos_y the y coordinate of the vector to be rotated
// @tparam number angle the angle by which the vector should be rotated
// @treturn number the X coordinate of the rotated vector
// @treturn number the Y coordinate of the rotated vector
int luafuncs_vector_rotate2d(lua_State* l) {
    const char func[] = "vector.rotate2d";
    if (lua_type(l, 1) != LUA_TNUMBER) {
        return haveluaerror(l, badargument1, 1, func, "number",
        lua_strtype(l, 1));
    }
    if (lua_type(l, 2) != LUA_TNUMBER) {
        return haveluaerror(l, badargument1, 2, func, "number",
        lua_strtype(l, 2));
    }
    if (lua_type(l, 3) != LUA_TNUMBER) {
        return haveluaerror(l, badargument1, 3, func, "number",
        lua_strtype(l, 3));
    }
    double x = lua_tonumber(l, 1);
    double y = lua_tonumber(l, 2);
    if (x == 0 && y == 0) {
        lua_pushnumber(l, 0);
        lua_pushnumber(l, 0);
        return 2;
    }
    double angle = lua_tonumber(l, 3);
    rotatevec(x, y, angle, &x, &y);
    lua_pushnumber(l, x);
    lua_pushnumber(l, y);
    return 2;
}