Simulation some times works bad.
I made a simple world simulation with a robot. But when i run the program, it sometimes works fine and sometimes works in a strange way. if the simulation runs fine at the begining then it maybe is posible to work fine. (try to run it several times and look the diferent ways it work)
thanks and I appreciate your help.
Note: To move the Car, try it with the arrow keys.
Can anybody try my simulacion code and explain me what is happend?
@use PhysicalControl.
@use Link.
@use Mobile.
@use Control.
@use Camera.
@use Shape.
@use Stationary.
@use Multibody.
@use Terrain.
@use Image.
@define CANTIDAD 4.
Controller UniversoVirtual.
PhysicalControl : UniversoVirtual {
+ variables:
vx, gx (float).
distancia (int).
angulo (float).
CantidadDPiedras (int).
posicionPiso, posicionLuz, posicionCentroCarro, posicionPuntaCarro (vector).
piso (object).
cielo (object).
LagoCongelado (object).
grass (object).
Valle (object).
Carro (object).
PiezasCarro (list).
PosPiedra (vector).
lucecita (object).
vision (object).
visionDireccion (vector).
+ to init:
CantidadDPiedras = 0.
self full-gravity.
self enable-fast-physics.
self set-fast-physics-iterations to 10.
piso = new Enlozado.
LagoCongelado = new Hielo.
grass = new Pasto.
Valle = new SuperficieVariable.
cielo = new Image.
cielo load from "\images\clouds.png".
self set-background-texture-image to cielo.
self disable-freed-instance-protection.
self enable-lighting.
self move-light to (0, 20 ,0).
self enable-shadow-volumes.
self enable-smooth-drawing.
self set-light-ambient-color to (0.6, 0.6, 0.7).
self set-light-diffuse-color to (0.5, 0.7, 0.7).
vision = new Camera.
vision set-size with-width 150 with-height 150.
vision set-position with-x 0 with-y 0.
lucecita = new Luz.
lucecita move to (100, 1.5, 50).
Carro = new Movil.
#Carro move to (0, 1, 0).
Carro move to (-40, 1, -78).
self offset-camera by (3, 13, -13).
self watch item Carro.
PiezasCarro = (Carro get-all-connected-links).
+ to iterate:
self drop-piedras.
if ((|(PiezasCarro{0} get-location)| < |(120, -0.2, 110)|) && (|(PiezasCarro{0} get-location)| > |(30, -0.2, 15)|)):
PiezasCarro{0} set-mu to 0.0.
if ((|(PiezasCarro{1} get-location)| < |(120, -0.2, 110)|) && (|(PiezasCarro{1} get-location)| > |(30, -0.2, 15)|)):
PiezasCarro{1} set-mu to 0.0.
if ((|(PiezasCarro{2} get-location)| < |(120, -0.2, 110)|) && (|(PiezasCarro{2} get-location)| > |(30, -0.2, 15)|)):
PiezasCarro{2} set-mu to 0.0.
if ((|(PiezasCarro{3} get-location)| < |(120, -0.2, 110)|) && (|(PiezasCarro{3} get-location)| > |(30, -0.2, 15)|)):
PiezasCarro{3} set-mu to 0.0.
posicionPiso = (piso get-location).
posicionLuz = (lucecita get-location).
posicionCentroCarro = (Carro get-location).
posicionPuntaCarro = (PiezasCarro{5} get-location).
#comPiedra = (Piedra get-all-connected-links).
distancia = (((| (Carro get-location) - (lucecita get-location) |)/10) - 0.5).
#masa = comPiedra{1} get-mass.
angulo = angle((Carro get-location), (lucecita get-location)).
self set-display-text number 1 to "Distancia Movil-Luz = $distancia m." at-x -.7 at-y -.95.
self set-display-text number 2 to "Angulo Movil-Luz = $angulo Rad." at-x -.7 at-y -.86.
self set-display-text number 3 to "posicion del Piso = $posicionPiso" at-x -.7 at-y -.76.
#self set-display-text numbre 2 to "Densidad aprox. piedra = $masa" at-x -.10 at-y -.85.
super iterate.
#if (distancia <= 0):{
#lucecita move to (Roca get-location + (5, 0, 5)).
#}
+ to post-iterate:
location (vector).
target (vector).
VDx, VDy, VDz (float).
#Donde VDx, VDy, VDz son las componentes del vector VisionDireccion.
target = (PiezasCarro{6} get-velocity).
location = (PiezasCarro{6} get-location).
VDx = dot(target, (1, 0 ,0)).
VDy = dot(target, (0, 1, 0)).
VDz = dot(target, (0, 0, 1)).
#print("El vector VDx es: $VDx").
#print("El vector VDy es: $VDy").
#print("El vector VDz es: $VDz").
#print("El vector completo es: $target").
#print("El otro vector location es: $location").
if |visionDireccion| <= 0: {
visionDireccion = target.
} else {
visionDireccion *= 0.5.
visionDireccion += target * 0.2.
}
if |visionDireccion| > 0.1: {
visionDireccion /= | visionDireccion |.
vision look at (visionDireccion + location) from -visionDireccion.
#print("VisionDireccion despues de todo es: $visionDireccion").
}
+ to drop-piedras:
if (CantidadDPiedras < 8):{
new Roca.
CantidadDPiedras++.
}
+ to decrementa:{
CantidadDPiedras--.
}
+ to catch-key-r-down:
+ to catch-key-up-down: {
vx++.
Carro controla-carro con-velocidad vx con-giro gx.
#print ("velocidad incrementada a: $vx").
}
+ to catch-key-left-down: {
gx = gx + 0.1.
Carro controla-carro con-velocidad vx con-giro gx.
#print("giro incrementado a: $gx").
}
# + to catch-key-left-up: {
# gx = gx - 0.1.
# Carro controla-carro con-velocidad vx con-giro gx.
# }
+ to catch-key-right-down: {
gx = gx - 0.1.
Carro controla-carro con-velocidad vx con-giro gx.
#print("giro decrementado a: $gx").
}
# + to catch-key-right-up: {
# gx = gx + 0.1.
# Carro controla-carro con-velocidad vx con-giro gx.
# }
+ to catch-key-down-down: {
vx--.
Carro controla-carro con-velocidad vx con-giro gx.
#print("velocidad decrementada a: $vx").
}
+ to catch-key-0x20-down: {
free Carro.
super destroy.
vx = 0.
gx = 0.
self init.
}
}
Stationary : Enlozado {
+ to init:
self register with-shape (new Cube init-with size (1000, 5, 1000))
at-location (0, -2.5, 0).
self catch-shadows.
self set-color to (0.8, 0.7, 0.5).
self set-e to 0.1.
self set-eT to 0.0.
self set-mu to 0.0.
}
Stationary : Hielo {
+ variables:
ice (object).
+ to init:
self register with-shape (new PolygonDisk init-with radius 50 sides 12 height .5)
at-location (80, -.2, 65).
ice = new Image.
ice load from "\images\snow.png".
self catch-shadows.
self set-texture-image to ice.
self set-e to 0.1.
self set-eT to 1.0.
self set-mu to 0.
+ to setup-collision-handler:
self handle-collisions with-type "Movil" with-method "llantas-resbalozas".
}
Stationary : Pasto {
+ variables:
gras (object).
+ to init:
self register with-shape (new PolygonDisk init-with radius 80 sides 7 height .5)
at-location (-100, -.2, -68).
gras = new Image.
gras load from "\images\grass.jpg".
self catch-shadows.
self set-texture-image to gras.
self set-mu to 100.
self set-eT to -5.0.
self set-e to 0.3.
}
Terrain : SuperficieVariable {
+ to init:
self generate-fractal-terrain with-ruggedness 0.2 with-height 25.5.
self move to (80, -.2, -150).
self set-scale to 1.5.
self set-mu to 10.
self set-eT to -5.0.
self set-e to 0.0.
}
MultiBody : Movil {
+ variables:
CuerpoRaiz (object).
links (list).
v, g (float).
joints (list).
masacuerpo (float).
masacamara (float).
masallanta, masacabina, masadefensa (float).
+ to get-root:
return CuerpoRaiz.
+ to init:
llanta (object).
Defensa (object).
Cabina (object).
FiguraCuerpo (object).
figuraCamara (object).
jointcamara (object).
jointDefensa (object).
jointCabina (object).
Defensa = (new miDef).
FiguraCuerpo = (new Cube init-with size (5, 3, 0.8)).
figuraCamara = (new Cube init-with size (2.3, 1.5, 0.6)).
llanta = (new PolygonDisk init-with radius 1.0 sides 90 height .4).
Cabina = (new Cube init-with size (2.5, 2.2, 1.0)).
links = 7 new Links.
joints = 4 new UniversalJoint.
jointcamara = 1 new FixedJoint.
jointDefensa = 1 new FixedJoint.
jointCabina = 1 new FixedJoint.
FiguraCuerpo set-mass to 10.
llanta set-mass to 1.
Defensa set-mass to 3.
Cabina set-mass to 2.
figuraCamara set-mass to 1.
masacuerpo = (FiguraCuerpo get-mass).
masacamara = (figuraCamara get-mass).
masallanta = (llanta get-mass).
masacabina = (Cabina get-mass).
masadefensa = (Defensa get-mass).
print("La masa de FiguraCuerpo es: $masacuerpo").
print("La masa de FiguraCamara es: $masacamara").
print("La masa de llanta es: $masallanta").
print("La masa de Cabina es: $masacabina").
print("La masa de Defensa es: $masadefensa").
# la masa total del carro es de 27.6 unidades---------------------------------------------------------
links{0} set-shape to llanta.
links{1} set-shape to llanta.
links{2} set-shape to llanta.
links{3} set-shape to llanta.
links{4} set-shape to figuraCamara.
links{5} set-shape to Defensa.
links{6} set-shape to Cabina.
self add-dependency on joints{0}.
self add-dependency on joints{1}.
self add-dependency on joints{2}.
self add-dependency on joints{3}.
self add-dependency on jointcamara.
self add-dependency on jointDefensa.
self add-dependency on jointCabina.
links set-color to (0.2, 0.2, 0.2).
links set-e to 0.3.
#links set-eT to 1.0.
links set-mu to 40.0.
CuerpoRaiz = new Link.
CuerpoRaiz set-shape to FiguraCuerpo.
self set-root to CuerpoRaiz.
CuerpoRaiz set-color to (0.2, 0.2, 0.2).
joints{0} link parent CuerpoRaiz to-child links{0}
with-normal (0, 1, 0)
with-parent-point (2.0, -1.8, 0)
with-child-point (0, 0, 0).
joints{1} link parent CuerpoRaiz to-child links{1}
with-normal (0, 1, 0)
with-parent-point (2.0, 1.8, 0)
with-child-point (0, 0, 0).
joints{2} link parent CuerpoRaiz to-child links{2}
with-normal (0, 1, 0)
with-parent-point (-2.0, -1.8, 0)
with-child-point (0, 0, 0).
joints{3} link parent CuerpoRaiz to-child links{3}
with-normal (0, 1, 0)
with-parent-point (-2.0, 1.8, 0)
with-child-point (0 , 0, 0).
jointcamara link parent CuerpoRaiz to-child links{4}
with-parent-point (2.5, 0.0, -0.5)
with-child-point (0, 0, 0).
jointDefensa link parent CuerpoRaiz to-child links{5}
with-parent-point (4.4, 0.0, 0.5)
with-child-point (0, 0, 0).
jointCabina link parent CuerpoRaiz to-child links{6}
with-parent-point (-0.3, 0.0, -0.8)
with-child-point (0, 0, 0).
self rotate around-axis (1, 0, 0) by 1.57.
self show-bounding-box.
jointcamara set-strength-limit to 0.0.
jointDefensa set-strength-limit to 0.0.
joints set-strength-limit to (joints get-strength-hard-limit).
joints{0} set-joint-limits a1-min -10 a1-max 30 a2-min -0.5 a2-max 0.5.
joints{1} set-joint-limits a1-min -10 a1-max 30 a2-min -0.5 a2-max 0.5.
joints{2} set-joint-limits a1-min -10 a1-max 30 a2-min 0 a2-max 0.
joints{3} set-joint-limits a1-min -10 a1-max 30 a2-min 0 a2-max 0.
+ to controla-carro con-velocidad vx (float) con-giro gx (float): {
v = vx.
g = gx.
}
+ to iterate:
joints{0} set-joint-velocity axis-1 v axis-2 g.
joints{1} set-joint-velocity axis-1 v axis-2 g.
joints{2} set-joint-velocity axis-1 v axis-2 0.
joints{3} set-joint-velocity axis-1 v axis-2 0.
+ to destroy:
free links.
free CuerpoRaiz.
free joints.
free v.
free g.
+ to llantas-resbalozas:
links set-mu to 0.0.
}
CustomShape : miDef {
+ variables:
vertices (8 vectors).
+ to init:
vertices[0] = (0.7, 0, 0).
vertices[1] = (-0.8, -2.2, 0).
vertices[2] = (-0.8, 2.2, 0).
vertices[3] = (-0.6, 0, -1.3).
vertices[4] = (-0.6, 0, 0.1).
vertices[5] = (0.5, 0, -1.1).
vertices[6] = (-0.8, -1.8, -1.1).
vertices[7] = (-0.8, 1.8, -1.1).
self add-face with-vertex-list { vertices[1], vertices[2], vertices[3] }.
self add-face with-vertex-list { vertices[0], vertices[1], vertices[4] }.
self add-face with-vertex-list { vertices[1], vertices[2], vertices[4] }.
self add-face with-vertex-list { vertices[0], vertices[2], vertices[4] }.
self add-face with-vertex-list { vertices[0], vertices[1], vertices[5] }.
self add-face with-vertex-list { vertices[0], vertices[2], vertices[5] }.
self add-face with-vertex-list { vertices[1], vertices[5], vertices[6] }.
self add-face with-vertex-list { vertices[3], vertices[5], vertices[6] }.
self add-face with-vertex-list { vertices[2], vertices[5], vertices[7] }.
self add-face with-vertex-list { vertices[3], vertices[5], vertices[7] }.
self add-face with-vertex-list { vertices[1], vertices[3], vertices[6] }.
self add-face with-vertex-list { vertices[2], vertices[3], vertices[7] }.
self finish-shape with-density 1.0.
}
MultiBody : Roca {
+ variables:
Cuerpo (object).
Enlaces (list).
joints (list).
l,m,n (int).
densidad (int).
Masa (float).
maparoca (Image object).
+ to init:
linkShape, EnlaceBajo, FiguraCuerpo (object).
vector_padre, vector_hijo (vector).
ox, oy, oz (int).
EnlaceBajo = (new Poligono).
linkShape = (new Poligono).
FiguraCuerpo = (new Cube init-with size (1.5, 1.0, 1)).
#linkShape set-mass to (1).
#links set-density to (1).
Enlaces = CANTIDAD new Links.
joints = CANTIDAD new FixedJoint.
densidad = random[10].
densidad = (((densidad * densidad)/1.5)+1).
FiguraCuerpo set-mass to 0.1.
EnlaceBajo set-mass to densidad.
linkShape set-mass to 0.1.
print("densidad = $densidad").
for l=0,l<CANTIDAD,l+=2: {
Enlaces{l} set-shape to linkShape.
Enlaces{l} set-rotation around-axis (1, 1, 1) by random[1.5].
}
for m=1,m<CANTIDAD,m+=2: {
Enlaces{m} set-shape to EnlaceBajo.
Enlaces{m} set-rotation around-axis (1, 1, 1) by random[1.5].
}
maparoca = (new Image load from "images/rock.png").
Enlaces set-texture-image to maparoca.
Cuerpo = new Link.
Cuerpo set-shape to FiguraCuerpo.
#Cuerpo set-color to (.5, .5, .5).
self set-root to Cuerpo.
Cuerpo set-texture-image to maparoca.
if ((densidad > 0) && (densidad < 2)):{
Enlaces set-color to(.9, .9, .9).
Cuerpo set-color to(.9, .9, .9).
}
else
if ((densidad > 2) && (densidad < 4)):{
Enlaces set-color to(.2, .5, .2).
Cuerpo set-color to(.2, .5, .2).
}
else
if ((densidad > 4) && (densidad < 8)):{
Enlaces set-color to(.3, .3, .5).
Cuerpo set-color to(.3, .3, .5).
}
else
if ((densidad > 8) && (densidad < 16)):{
Enlaces set-color to(.4, .7, .7).
Cuerpo set-color to(.4, .7, .7).
}
else
if ((densidad > 16) && (densidad <32)):{
Enlaces set-color to(.5, .5, .5).
Cuerpo set-color to(.5, .5, .5).
}
else
if ((densidad > 32) && (densidad < 64)):{
Enlaces set-color to(.6, .2, .9).
Cuerpo set-color to(.6, .2, .9).
}
else
if ((densidad > 64) && (densidad < 100)):{
Enlaces set-color to(.7, .5, .1).
Cuerpo set-color to(.7, .5, .1).
}
else
if (densidad > 100):{
Enlaces set-color to(.3, .3, .3).
Cuerpo set-color to(.3, .3, .3).
}
Masa = FiguraCuerpo get-mass.
print("masa = $Masa").
for n=0,n<CANTIDAD,n+=2: {
# los valores originales de x = 2.5, y = 2.0, z = 2.5.
ox = random[1].
oy = random[1].
oz = random[1].
if (self can-respond to (self check-self-penetration)): {
self enable-self-collisions.
ox = random[1].
oy = random[1].
oz = random[1].
}
else {
joints{n} link parent Cuerpo to-child Enlaces{n}
with-parent-point random[(ox, oy, oz)]
with-child-point random[(ox, oz, oy)].
joints{n} set-relative-rotation around-axis (1, 1, 1) by random[2].
joints{n+1} link parent Enlaces{n} to-child Enlaces{n+1}
with-parent-point random[(ox, oy, oz)]
with-child-point random[(oz, ox, oy)].
print("los valores son ox: $ox - Oy: $oy - oz: $oz").
joints{n+1} set-relative-rotation around-axis (1, 1, 1) by random[2].
}
}
self register with-link Cuerpo.
joints set-strength-limit to (joints get-strength-hard-limit).
#self enable-self-collisions.
#self check-self-penetration.
self move to (random[(150, 20, 150)] - (50, 1, 50)).
+ to center:
self move to (0, 10, 0).
+ to iterate:
self schedule method-call "destruye" at-time ((controller get-time) + 150.0).
+ to destruye:{
controller decrementa.
free self.
}
}
Mobile : LaFigura {
+ variables:
imagenPiedra (object).
+ to init:
self set-shape to (new Poligono).
self set-color to (1, 1, 1).
self enable-physics.
imagenPiedra = (new Image load from "images/rock.png").
self set-texture-image to imagenPiedra.
}
CustomShape : Poligono {
+ variables:
vertices (5 vectors).
+ to init:
# the triangular "base" of the shape
vertices[0] = (-1, 0, 0).
vertices[1] = (1.4, 0, 1.4).
vertices[2] = (1.4, 0, -1.4).
vertices[3] = (0, 1.4, 0).
vertices[4] = (0, -1.4, 0).
self add-face with-vertex-list { vertices[0], vertices[1], vertices[3] }.
self add-face with-vertex-list { vertices[1], vertices[2], vertices[3] }.
self add-face with-vertex-list { vertices[0], vertices[2], vertices[3] }.
self add-face with-vertex-list { vertices[0], vertices[1], vertices[4] }.
self add-face with-vertex-list { vertices[1], vertices[2], vertices[4] }.
self add-face with-vertex-list { vertices[0], vertices[2], vertices[4] }.
self finish-shape with-density 1.0.
}
Mobile : Luz {
+ variables:
lightmap (Image object).
+ to init :
self set-shape to (new Shape init-with-sphere radius 2).
lightmap = (new Image load from "images/lightmap.png").
self set-lightmap-image to lightmap.
self set-color to (1, 1, 1).
}

Simulation some times works bad.
It took me quite some time to get this sim to run, it seems like I may be missing something, but it is hard to tell. I had to add and change a few things to get it running on my machine. It runs, but very slow. It won't even iterate. Could you re-post the code from a running version?
Simulation some times works bad.
Sorry :lol: ..... but i load a version of code that doesn´t work. I will reload the code in the upper window. Thank´s for your help. [/code]
Simulation some times works bad.
I got it working this time, thanks for reloading the code. I ran the sim, and my keys do not effect it. Don't worry, I know this isn't a problem with your code :wink:
I am using the 2.4 IDE breve in Windows. How do I set it up to take in key presses? I can't get it working. Sorry I'm no help yet!
PS: By the way, the sim does look very nice. Hope I can help once I get the dang thing working.
Simulation some times works bad.
I made a chage in the upper code that will maybe make it run in your computer. I just put as comments the methods key-upper-case
I hope that you don´t afraid of the next list of problem stuff and will thank you for your help.
can you try it again and see what is happend?. One of mi problems is that the stones jumps without a reason, another of the problems is that the wheels of the vehicle after a collide they bend or twist in a manner that losse their normal orientation or position. Another problem is that sometimes the sim doesn´t make what is spected (eg. in first or second try of runnig it do not respond, some variables take unespected values, incorrects, among others).
# + to catch-key-left-up: { # gx = gx - 0.1. # Carro controla-carro con-velocidad vx con-giro gx. # }Simulation some times works bad.
That change did the trick! I can now operate the simulation fine, and have run it a few times to see what happens. It seems to run well the first time I test it, but after that (stop and play again) it seems to run slower, then just splutter almost to a halt. I don't know why, though, since there are no objects that are saved, etc. It may be a memory leak in 2.4. I remember seeing a bug listed like this, and heard that it has been fixed, but there is a chance that it still exists somewhere else (or maybe we're running an old version.. I'll check mine).
I've noticed the rocks appear randomly, as you intended. This includes a random 'z' value, so they fall from the sky. Once they hit ground, they bounce back up, sometimes by a substantial amount. I did not see them hop up into the air unprovoked, but if they later on bounce up again, it could be a fluke of the 'fast-physics' which is not as accurate, and may cause them to jump into the air seemingly without reason. I would think that by dropping, they eventually would bounce and bounce and bounce until they are barely moving. Then, when the movements are very small and become more difficult for the program to control, they may bounce, at a tiny angle, and over-estimate the calculation. That, or they also may be reversing their velocity. If an object appears partially through the floor plane, they often exhibit very peculiar properties, such as leaping off into the sky, vanishing, or dropping straight through. There are a few reasons more, and I'm sure even more that I don't know about (I'm certainly not an expert). Maybe check, though?
As for the wheels, you may want to include some basic error correction in the vehicle's links.
<psuedo-code>
if (link-angle-for-wheel-axis != proper-angle)
{
reset link-angle to proper-angle.
}
</psuedo-code>
..something akin to that. I'll continue to check through your code, though, and see what I can find. I apologise for the slowness in my checking, but I couldn't help but notice that your code is in Spanish. While I can decipher most of it without any problems, I had to ask a co-worker who speaks Spanish about a couple of them. Don't worry about changing it, I've got it now, it just takes a bit of time since it seems, to me, hard to follow.
I'll see what I can find!
Simulation some times works bad.
Thank´s for your help i will try your changes....
:) and write to you back.