We have seen various physical, electrical, and fluid formulas described in DYNAMIC evolve within the SOLVE.
In summary, we introduced all the formulas that a system had to satisfy to be solved simultaneously. This allowed us to solve, for example, the problem of 3 connected liquid tanks.
Now we will see a way to use that same resource to solve physics with constraints, which is nothing more than what we see in video games when two objects collide. Similarly, we can simulate multiple objects moving among themselves with certain rotation or distance constraints.
We are not introducing a new type of simulation, but a different way to describe another problem.
To get an idea of how it works, it is like any other simulation: we must observe what happens in the real world and then describe it as it is.
The steps are usually as follows:
1.- The object or objects. To simplify, we will use perfect spheres and cubes. They can have a weight and radius. They are placed in a location in space.
2.- Its point velocity. We must know if it is static, moving with a certain free velocity, or subjected to a force that gives it acceleration. That is:
We will integrate the position:
x = x + vΔt
And if forces are involved, they also come into play:
v = v + aΔt
3.- Constraint detection. If we are talking about an object hitting a wall, we must know this before effecting any consequence. Therefore, we check if we are violating any constraints.
If our object were a sphere, we check if it hits or invades the space of:
Wall: compare coordinates
Sphere: distance between centers
Box: compare intervals (AABB)
Complex shapes: more geometry
4.- Constraints. While we have discussed position and velocity, a constraint, whatever it may be, must modify both characteristics. If the object hits a wall, the increment of x will go in the opposite direction and the velocity will change sign. A constraint does not "prevent," it corrects.
This doesn't exclude the possibility of a constraint being, for example, not exceeding a certain speed.
5.- Scaling. When there are many objects, it becomes unfeasible to put thousands of objects into the system at once, so it is done in parts.
Filtering candidates using grid or tree methods is required...
The Example:
A single spherical mass bouncing against the walls that enclose it.
We show the formulas for constant velocities and their integrals to obtain the positions.
The states are the 4 mentioned: Position (x,y) and velocities (vx,vy).
And following this, the constraints. Let's look at the first one:
{
"VARIABLE":"VX_PREV",
"EXPRESSION":"(X <= (MINX+R) and VX < 0) ? -VX*E : ((X >= (MAXX-R) and VX > 0) ? -VX*E : VX)"
}
This indicates what we will do with a specific variable if certain conditions occur. In this case, what will happen to the variable "VX_PREV", which is one of the states passed to the next solver iteration.
The *_PREV variables represent the state that the next SOLVE step "inherits".
The result will either be leaving it as it currently is (VX) or modifying it if the established conditions are met.
If we are moving left and have passed the left boundary, VX_PREV will be -VX*E.
Otherwise, we check the right boundary, setting VX_PREV = -VX*E again.
And as mentioned, if no constraints apply, VX_PREV will be VX.
Note that "E" is the velocity coefficient conserved after each bounce (restitution), so it is used as a multiplier to reduce speed. If E were 0, it would be a dead stop.
Example
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
SYSTEM {TYPE:VISUAL, MODE:3D, V_WIDTH:70, V_HEIGHT:50, CAMERA:0}
SYSTEM {type:OPTIONS, TIME_DECIMALS:1, SPEED:5}
;SYSTEM {TYPE:OPTIONS, REAL_TIME:1}
Graphic {NAME:line1,Type:LINE,X1:0,Y1:0,X2:0,Y2:500}
Graphic {NAME:line2,Type:LINE,X1:0,Y1:0,X2:500,Y2:0}
Graphic {NAME:line3,Type:LINE,X1:0,Y1:0,X2:0,Y2:0,Z2:500}
GRAPHIC {NAME:mass, TYPE:SPHERE, X:0, Y:3, Z:0, radius:3, color:red}
GRAPHIC {NAME:supportLeft, TYPE:BOX, X:-1, Y:30, Z:0, WIDTH:2, HEIGHT:60, DEPTH:20, color:yellow}
GRAPHIC {NAME:supportRight, TYPE:BOX, X:31, Y:30, Z:0, WIDTH:2, HEIGHT:60, DEPTH:20, color:yellow}
GRAPHIC {NAME:supportTop, TYPE:BOX, X:15, Y:61, Z:0, WIDTH:30, HEIGHT:2, DEPTH:20, color:yellow}
GRAPHIC {NAME:supportBottom, TYPE:BOX, X:15, Y:-1, Z:0, WIDTH:30, HEIGHT:2, DEPTH:20, color:yellow}
INITIAL ball2D_config, {
EXPRESSIONS: [
; constant velocity (no forces)
"VX - VX_PREV",
"VY - VY_PREV",
; explicit integration
"X - X_PREV - DT * VX",
"Y - Y_PREV - DT * VY"
],
STATES: [
"X","Y","VX","VY"
],
VARIABLES: [
"X_PREV","Y_PREV","VX_PREV","VY_PREV",
"R","E",
"MINX","MAXX","MINY","MAXY"
],
; CONDITIONS correct the state after the free step (move → correct).
CONDITIONS: [
; --- left/right wall ---
{
"VARIABLE":"VX_PREV",
"EXPRESSION":"(X <= (MINX+R) and VX < 0) ? -VX*E : ((X >= (MAXX-R) and VX > 0) ? -VX*E : VX)"
},
{
"VARIABLE":"X_PREV",
"EXPRESSION":"(X <= (MINX+R) and VX < 0) ? (MINX+R) : ((X >= (MAXX-R) and VX > 0) ? (MAXX-R) : X)"
},
; --- bottom/top wall ---
{
"VARIABLE":"VY_PREV",
"EXPRESSION":"(Y <= (MINY+R) and VY < 0) ? -VY*E : ((Y >= (MAXY-R) and VY > 0) ? -VY*E : VY)"
},
{
"VARIABLE":"Y_PREV",
"EXPRESSION":"(Y <= (MINY+R) and VY < 0) ? (MINY+R) : ((Y >= (MAXY-R) and VY > 0) ? (MAXY-R) : Y)"
}
]
}
INITIAL ball2D_values, {
R: 3,
E: 0.8,
MINX: 0, MAXX: 30,
MINY: 0, MAXY: 60,
X_PREV: 20,
Y_PREV: 20,
VX_PREV: 12,
VY_PREV: 7
}
DYNAMIC {
name: box2d,
CONFIG: V$ball2D_config,
VALUES: V$ball2D_values,
X: 200,
Y: 200
}
START 1000
include ./library_graphics/speedometer.lib
;==============================================================
PROCEDURE agent.init
while (1==1)
SOLVE { name:"box2d", DT: 0.1, SAVEVALUE: "result"}
SAVEVALUE X, X$(result.X)
SAVEVALUE Y, X$(result.Y)
; === Mass position
MOVE {name:mass, X:X$X , Y:X$Y}
assign vx,round(X$(result.VX),3)
assign vy,round(X$(result.VY),3)
CALL ind1.speedometer.set, P$vx
CALL ind2.speedometer.set, P$vy
; STOP CONDITION
; IF (ABS(P$vx) < 0.001)
; stop
; ENDIF
advance 0.1,0
endwhile
ENDPROCEDURE
;====================================================================
PROCEDURE PRE_RUN
CALL create_indicators
TIMEOUT agent.init,0
TERMINATE_VE
ENDPROCEDURE
;=================================================
PROCEDURE create_indicators
assign config,{title:"VX"
,x:50,y:15
,width:5 ,height:18
,value:0
,min_value:-15
,max_value:15
,"color":"#ff0000"
,font:"3px"}
call ind1.speedometer.init,V$config
assign config,{title:"VY"
,x:80,y:15
,width:5 ,height:18
,value:0
,min_value:-15
,max_value:15
,"color":"blue"
,font:"3px"}
call ind2.speedometer.init,V$config
ENDPROCEDURE
;=================================================