Jump to content

How to create objects and render Collision Shape by script in simulation?


photo

Recommended Posts

Good evening,

I want to create objects from script and render their bounds. How may I do that? This is the current code I wrote.

	public ObjectDummy physicsDummy;
	ShapeSphere collisionShape;
	void Init(){
		Visualizer.Enabled = true;
		collisionShape = new ShapeSphere(1f);
		physicsDummy.Body.AddShape(collisionShape);
		physicsDummy.BoundSphere.Set(new BoundSphere(physicsDummy.WorldPosition,1));
	}
	void Update() {
		Visualizer.RenderBoundSphere(physicsDummy.BoundSphere, physicsDummy.WorldTransform, vec4.BLUE);
	}

 

Edited by Sevdat
Link to comment
Posted (edited)

Good morning Sevdat,

To generate a primitive you need the code here: https://developer.unigine.com/en/docs/future/start/quick_start/physical_objects/index?rlang=cs

Sphere example:

	void Init(){
			ObjectMeshDynamic sphere = Primitives.CreateSphere(0.5f, 9, 32);
			sphere.TriggerInteractionEnabled = true;
			sphere.SetIntersection(true, 0);
			sphere.SetIntersectionMask(0x00000080, 0); // check the BulletIntersection bit
			sphere.WorldTransform = MathLib.Translate(new vec3(4.5f, 5.5f, 1.0f));
			sphere.SetMaterialPath("materials/mesh_base_1.mat", "*");
			sphere.Name = "sphere";
			BodyRigid bodySphere = new BodyRigid(sphere);
			new ShapeSphere(bodySphere, 0.5f);
			bodySphere.ShapeBased = false;
			bodySphere.Mass = 2.0f;
	}

To Generate primitive meshes by code use this: https://developer.unigine.com/en/docs/future/code/usage/mesh_class/index?rlang=cs#creating_box

P.S Is there a less complicated way of doing it? and I still haven't figured out how to render Shape for physics in the sumulation mode. Not the Helper Physics one. I saw that in the tutorial. I mean when pressing this button how do I render the collision area added to Shapes?:

image.png.83451b1f6de41c9dd8af0963be8692e2.png

image.png.80626452c93c9addfe40e93df5d87a5c.png

Also I want to generate joints by script. I saw the documentation, but don't know how to attach different objects by script: https://developer.unigine.com/en/docs/latest/principles/physics/joints/

Edited by Sevdat
Link to comment
Posted (edited)

Good morning,

I've created something like this to create triangles and animate their vertexes, but I don't quite understand how everything works so I have a few questions.

	Mesh mesh;
	ObjectMeshDynamic dynamicMesh;
	vec3[] vertexes = new vec3[]{
		new vec3(-0.5f, -0.5f, -0.5f),new vec3(0.5f, -0.5f, -0.5f),new vec3(-0.5f, 0.5f, -0.5f),
		new vec3(-0.5f, -1.5f, -0.5f),new vec3(0.5f, -1.5f, -0.5f),new vec3(-0.5f, 1.5f, -0.5f)
	};
	int[] indices = new int[]{0,1,2,3,4,5,2,1,0,5,4,3};
	private void Init(){
		mesh = new Mesh();
		int surface_index = mesh.AddSurface($"surface_{0}");
		drawTriangle(mesh,surface_index,vertexes,indices);
		dynamicMesh = new ObjectMeshDynamic(mesh);
		mesh.Clear();
		dynamicMesh.WorldPosition = new vec3(0,0,5);
	}
	private void drawTriangle(Mesh mesh, int surface_index, vec3[] vertexArray, int[] indeces){
		for (int i = 0; i < vertexArray.Length; i++) mesh.AddVertex(vertexArray[i], surface_index);
		for (int i = 0; i < indeces.Length; i++) mesh.AddIndex(indeces[i], surface_index);
		mesh.CreateBounds(surface_index);
	}
	private void updateTriangle(ObjectMeshDynamic triangle, int vertex,float moveSpeed){
		triangle.SetVertex(vertex,triangle.GetVertex(vertex)*moveSpeed);
		triangle.FlushVertex();
	}
	
	void Update() {
		updateTriangle(dynamicMesh,0,1.0001f);
	}

What does this part of the code exactly do? Why is it an int if a string is passed? Does it allocate a small enviroment where meshes can exist? Does a single mesh only have a single surface name?

int surface_index = mesh.AddSurface($"surface_{0}");

When is it appropriate to use:

mesh.Clear();

Is using this code efficient to update the mesh of the object or is there a better way of doing this?

How can I get the Array of the mesh containing the indexes and vertexes?

Edited by Sevdat
Link to comment
  • Sevdat changed the title to How to create objects and render Collision Shape by script in simulation?
×
×
  • Create New...