ARGoS 3
A parallel, multi-engine simulator for swarm robotics
dynamics2d_box_model.cpp
Go to the documentation of this file.
1
9#include "dynamics2d_engine.h"
10
11namespace argos {
12
13 /****************************************/
14 /****************************************/
15
17 CBoxEntity& c_entity) :
18 CDynamics2DStretchableObjectModel(c_engine, c_entity) {
19 /* Get the size of the entity */
20 CVector3 cHalfSize = c_entity.GetSize() * 0.5f;
21 /* Create a polygonal object in the physics space */
22 /* Start defining the vertices
23 NOTE: points must be defined in a clockwise winding
24 */
25 cpVect tVertices[] = {
26 cpv(-cHalfSize.GetX(), -cHalfSize.GetY()),
27 cpv(-cHalfSize.GetX(), cHalfSize.GetY()),
28 cpv( cHalfSize.GetX(), cHalfSize.GetY()),
29 cpv( cHalfSize.GetX(), -cHalfSize.GetY())
30 };
32 CRadians cXAngle, cYAngle, cZAngle;
33 GetEmbodiedEntity().GetOriginAnchor().Orientation.ToEulerAngles(cZAngle, cYAngle, cXAngle);
34 /*
35 * Create body and shapes
36 */
37 cpBody* ptBody;
38 if(GetEmbodiedEntity().IsMovable()) {
39 /* The box is movable */
40 SetMass(c_entity.GetMass());
41 /* Create the body */
42 ptBody =
43 cpSpaceAddBody(GetDynamics2DEngine().GetPhysicsSpace(),
44 cpBodyNew(GetMass(),
45 cpMomentForPoly(GetMass(),
46 4,
47 tVertices,
48 cpvzero)));
49 ptBody->p = cpv(cPosition.GetX(), cPosition.GetY());
50 cpBodySetAngle(ptBody, cZAngle.GetValue());
51 /* Create the shape */
52 cpShape* ptShape =
53 cpSpaceAddShape(GetDynamics2DEngine().GetPhysicsSpace(),
54 cpPolyShapeNew(ptBody,
55 4,
56 tVertices,
57 cpvzero));
58 ptShape->e = 0.0; // no elasticity
59 ptShape->u = 0.7; // lots contact friction to help pushing
60 /* The shape is grippable */
62 ptShape));
63 /* Set the body so that the default methods work as expected */
64 SetBody(ptBody, c_entity.GetSize().GetZ());
65 /* Friction with ground */
68 }
69 else {
70 /* The box is not movable */
71 /* Create a static body */
72 ptBody = cpBodyNewStatic();
73 ptBody->p = cpv(cPosition.GetX(), cPosition.GetY());
74 cpBodySetAngle(ptBody, cZAngle.GetValue());
75 /* Create the shape */
76 cpShape* ptShape =
77 cpSpaceAddShape(GetDynamics2DEngine().GetPhysicsSpace(),
78 cpPolyShapeNew(ptBody,
79 4,
80 tVertices,
81 cpvzero));
82 ptShape->e = 0.0; // No elasticity
83 ptShape->u = 0.1; // Little contact friction to help sliding away
84 /* This shape is normal (not grippable, not gripper) */
85 ptShape->collision_type = CDynamics2DEngine::SHAPE_NORMAL;
86 /* Set the body so that the default methods work as expected */
87 SetBody(ptBody, c_entity.GetSize().GetZ());
88 }
89 }
90
91 /****************************************/
92 /****************************************/
93
95 /* Remove existing shape */
96 cpSpaceRemoveShape(GetDynamics2DEngine().GetPhysicsSpace(),
97 GetBody()->shapeList);
98 /* Calculate new shape size */
99 CVector3 cHalfSize = c_size * 0.5f;
100 cpVect tVertices[] = {
101 cpv(-cHalfSize.GetX(), -cHalfSize.GetY()),
102 cpv(-cHalfSize.GetX(), cHalfSize.GetY()),
103 cpv( cHalfSize.GetX(), cHalfSize.GetY()),
104 cpv( cHalfSize.GetX(), -cHalfSize.GetY())
105 };
106 /* Create the shape */
107 cpShape* ptShape =
108 cpSpaceAddShape(GetDynamics2DEngine().GetPhysicsSpace(),
109 cpPolyShapeNew(GetBody(),
110 4,
111 tVertices,
112 cpvzero));
113 if(GetEmbodiedEntity().IsMovable()) {
114 /* The box is movable */
115 ptShape->e = 0.0; // no elasticity
116 ptShape->u = 0.7; // lots contact friction to help pushing
117 /* The shape is grippable */
119 delete GetGrippable();
121 ptShape));
122 }
123 else {
124 /* The box is not movable */
125 ptShape->e = 0.0; // No elasticity
126 ptShape->u = 0.1; // Little contact friction to help sliding away
127 /* This shape is normal (not grippable, not gripper) */
128 ptShape->collision_type = CDynamics2DEngine::SHAPE_NORMAL;
129 }
130 /* Set the body so that the default methods work as expected */
131 SetBody(GetBody(), c_size.GetZ());
132 }
133
134 /****************************************/
135 /****************************************/
136
138
139 /****************************************/
140 /****************************************/
141
142}
#define REGISTER_STANDARD_DYNAMICS2D_OPERATIONS_ON_ENTITY(SPACE_ENTITY, DYN2D_ENTITY)
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
const SAnchor & GetOriginAnchor() const
Returns a const reference to the origin anchor associated to this entity.
CQuaternion Orientation
The orientation of the anchor wrt the global coordinate system.
CVector3 Position
The position of the anchor wrt the global coordinate system.
CEmbodiedEntity & GetEmbodiedEntity()
Returns the embodied entity associated to this physics model.
It defines the basic type CRadians, used to store an angle value in radians.
Definition angles.h:42
Real GetValue() const
Returns the value in radians.
Definition angles.h:111
void ToEulerAngles(CRadians &c_z_angle, CRadians &c_y_angle, CRadians &c_x_angle) const
Definition quaternion.h:172
A 3D vector class.
Definition vector3.h:31
Real GetX() const
Returns the x coordinate of this vector.
Definition vector3.h:105
Real GetY() const
Returns the y coordinate of this vector.
Definition vector3.h:121
Real GetZ() const
Returns the z coordinate of this vector.
Definition vector3.h:137
Real GetMass() const
Definition box_entity.h:86
const CVector3 & GetSize() const
Definition box_entity.h:80
void Resize(const CVector3 &c_size)
CDynamics2DBoxModel(CDynamics2DEngine &c_engine, CBoxEntity &c_entity)
cpFloat GetBoxAngularFriction() const
cpFloat GetBoxLinearFriction() const
CDynamics2DEngine & GetDynamics2DEngine()
Returns the dynamics 2D engine state.
virtual void SetBody(cpBody *pt_body, Real f_height)
Sets the body and registers the default origin anchor method.
cpBody * GetBody()
Returns the body as non-const pointer.
A stretchable and grippable object model for the dynamics 2D engine.
void SetLinearFriction(Real f_max_bias, Real f_max_force)
Sets the linear friction of this object.
void SetGrippable(CDynamics2DGrippable *pc_grippable)
void SetAngularFriction(Real f_max_bias, Real f_max_force)
Sets the angular friction of this object.