vp_object_add

From Virtual Paradise Wiki
Revision as of 23:03, 16 December 2016 by Sleepy E (talk | contribs) (Added euler to angle-axis conversion code)
Jump to navigation Jump to search
Method call snippet vp_object_add(instance);

Build a new object.

Parameters

These are the parameters that this method requires:

Parameter Usage
VPInstance
instance
Pointer to the instance this method call is intended for

Used attributes

This method uses data set in these attributes when called:

Attribute Usage
VP_OBJECT_TYPE Type of object (0 - 3D Model, 2 - Particle emitter, 3 - Path object)
VP_OBJECT_X X coordinate
VP_OBJECT_Y Y coordinate
VP_OBJECT_Z Z coordinate
VP_OBJECT_ROTATION_X Rotation X axis
VP_OBJECT_ROTATION_Y Rotation Y axis
VP_OBJECT_ROTATION_Z Rotation Z axis
VP_OBJECT_ROTATION_ANGLE Rotation angle in radians
VP_OBJECT_YAW Yaw angle in degrees (these are used instead of X,Y,Z axis when rotation angle is set to INFINITY)
VP_OBJECT_PITCH Pitch angle in degrees
VP_OBJECT_ROLL Roll angle in degrees
VP_OBJECT_MODEL Model
VP_OBJECT_ACTION Action
VP_OBJECT_DESCRIPTION Description
VP_OBJECT_DATA Data

Returns

This method returns a return code integer, which indicates whether the call was successful or errored for any reason:

Return code Cause
VP_RC_SUCCESS Successful call (for methods that have a registered callback, it only means the request has been sent)
VP_RC_NOT_IN_WORLD Bot is not currently in a world

The following attributes will be returned in VP_CALLBACK_OBJECT_ADD if the operation was successful:

Attribute Usage Also returned for
VP_OBJECT_ID Object id that was assigned to the new object

Behavior

  • Upon successful creation of an object, VP_EVENT_OBJECT is triggered for those who have subscribed to it.
  • If VP_OBJECT_ROTATION_ANGLE is set to INFINITY then it will be stored as that in world. The euler angles will be not be converted to an equivalent axis-angle representation. See examples for euler to axis-angle conversion code.

Examples

Build a street1.rwx object at 0X 0Y 0Z.

void handle_object_add(VPInstance sdk, int rc, int ref)
{
  if (rc)
    printf("Couldn't build object (reason %d)\n", rc);
  else
    printf("Object built successfully!\n");
}

int main(int argc, const char* argv[])
{
  //...
  
  vp_callback_set(sdk, VP_CALLBACK_OBJECT_ADD, handle_object_add);

  vp_int_set(sdk, VP_OBJECT_TYPE, 0); // 3D Model
  vp_float_set(sdk, VP_OBJECT_X, 0.0);
  vp_float_set(sdk, VP_OBJECT_Y, 0.0);
  vp_float_set(sdk, VP_OBJECT_Z, 0.0);
  vp_float_set(sdk, VP_OBJECT_ROTATION_X, 0.0);
  vp_float_set(sdk, VP_OBJECT_ROTATION_Y, 0.0);
  vp_float_set(sdk, VP_OBJECT_ROTATION_Z, 0.0);
  vp_float_set(sdk, VP_OBJECT_ROTATION_ANGLE, 0.0);
  vp_string_set(sdk, VP_OBJECT_MODEL, "street1.rwx");
  vp_string_set(sdk, VP_OBJECT_ACTION, "");
  vp_string_set(sdk, VP_OBJECT_DESCRIPTION, "");
	
  rc = vp_object_add(sdk);
  if (rc)
    printf("Couldn't build object (reason %d)\n", rc);

  //...
}

Convert euler angles (in degrees) to axis-angle representation.

void rotate(double yaw, double pitch, double roll, double *x, double *y, double *z, double *angle)
{
  // Convert angles to radians
	
  yaw = (yaw / 180.0) * M_PI;
  pitch = (pitch / 180.0) * M_PI;
  roll = (roll / 180.0) * M_PI;

  // Convert to axis-angle

  double sin_yaw_2, sin_pitch_2, sin_roll_2, cos_yaw_2, cos_pitch_2, cos_roll_2;

  sin_yaw_2 = sin(yaw / 2);
  sin_pitch_2 = sin(pitch / 2);
  sin_roll_2  = sin(roll / 2);
  cos_yaw_2   = cos(yaw / 2);
  cos_pitch_2 = cos(pitch / 2);
  cos_roll_2  = cos(roll / 2);

  // Return a normalized axis

  double x_tmp, y_tmp, z_tmp;

  x_tmp = sin_yaw_2 * sin_roll_2 * cos_pitch_2 + cos_yaw_2 * cos_roll_2 * sin_pitch_2;
  y_tmp = sin_yaw_2 * cos_roll_2 * cos_pitch_2 + cos_yaw_2 * sin_roll_2 * sin_pitch_2;
  z_tmp = cos_yaw_2 * sin_roll_2 * cos_pitch_2 - sin_yaw_2 * cos_roll_2 * sin_pitch_2;

  double norm = sqrt(x_tmp * x_tmp + y_tmp * y_tmp + z_tmp * z_tmp);

  if (norm)
  {
    *x = x_tmp / norm;
    *y = y_tmp / norm;
    *z = z_tmp / norm;
  }
  else
  {
    *x = 1;
    *y = 0;
    *z = 0;
  }

  *angle = 2 * acos(cos_yaw_2 * cos_pitch_2 * cos_roll_2 - sin_yaw_2 * sin_pitch_2 * sin_roll_2);
}

See also