Jump to content

Rigidbody water interaction.


Recommended Posts

I have a mesh with a rigidbody and a global water object how can i make this mesh interact with the waves of the global water object

Edited by Wimo135
Link to comment

Thank you for your response. This is helpful but i already knew this, i would like to simulate the waves on the whole global water body/

Link to comment

Wimo135

That would be impossible to achieve with the current hardware, unfortunately. You can use ObjectWaterGlobal API to get the wave height at the specific points and implement simplified ship buoyancy logic based on the information you get.

If you are using C++ you can check SDK Browser -> Demos -> CPP Samples -> Water Global with this approach implementation. For C# right now we don't have similar sample available out of the box, but it shouldn't be hard to port this code.

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment

Ive been trying to integrate this into my project recently but im running into a problem where my code just outputs a seemingly random number.
This is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;
using UnigineApp;
[Component(PropertyGuid = "c0bb98fbb56cba1ef20a5ae58789cf003de8765f")]
public class WaveDetector : Component
{
    public ObjectWaterGlobal water;
    public NodeDummy node;
    public float z;
    public Unigine.Object boat;
    private void Init()
    {
        // write here code to be called on component initialization
 
    }
   
    private void Update()
    {
        // write here code to be called before updating each render frame
        z = water.FetchHeight(node.Position);
        Log.Error(z);
        //boat.Position = new vec3(boat.Position.x, boat.Position.y, boat.Position.z + z);
    }
}
Link to comment

I have been able to get the boat to move over the waves but i cant figure out how to make the boat rotate to the waves. for example if a wave is in front of the boat it should rotate up then go over the wave.

Link to comment

Hi
in BuoyComponent.cpp

	if (water)
	{
		auto node_transform = node->getWorldTransform(); // current transform matrix: (TRS: translate-rotation-scale)
		
      // point0, point1, point2 - position of control points
		Vec3 point0 = point_front_center->getWorldPosition();
		Vec3 point1 = point_back_left->getWorldPosition();
		Vec3 point2 = point_back_right->getWorldPosition();

		//create basis before transformation by 3 point
		Vec3 tmp_z = normalize(cross(normalize(point1 - point0), normalize(point2 - point0))); // up vector
		if (getAngle(vec3(tmp_z), vec3_up) > 90)
			tmp_z = -tmp_z;
		Vec3 tmp_y = normalize(point1 - point0); 
		Vec3 tmp_x = tmp_y;
		cross(tmp_x, tmp_y, tmp_z).normalize(); // right vector
		cross(tmp_y, tmp_z, tmp_x).normalize(); // forward
		Mat4 old_basis;
		old_basis.setTranslate(point0);
		old_basis.setColumn3(0, tmp_x);
		old_basis.setColumn3(1, tmp_y);
		old_basis.setColumn3(2, tmp_z);


		// find height for each controll point
		Scalar h0 = water->fetchHeight(Vec3(point0.x, point0.y, 0.0f));
		Scalar h1 = water->fetchHeight(Vec3(point1.x, point1.y, 0.0f));
		Scalar h2 = water->fetchHeight(Vec3(point2.x, point2.y, 0.0f));

		Scalar lerp_k = Game::getIFps() * mass_coef;
		
      	// smooth change height for each control point 
		point0.z = lerp(point0.z, h0, lerp_k);
		point1.z = lerp(point1.z, h1, lerp_k);
		point2.z = lerp(point2.z, h2, lerp_k);

		// calculate new basis the same way
		tmp_z = normalize(cross(normalize(point1 - point0), normalize(point2 - point0)));
		tmp_y = normalize(point1 - point0);
		cross(tmp_x, tmp_y, tmp_z).normalize();
		cross(tmp_y, tmp_z, tmp_x).normalize();
		Mat4 new_basis;
		new_basis.setTranslate(point0);
		new_basis.setColumn3(0, tmp_x);
		new_basis.setColumn3(1, tmp_y);
		new_basis.setColumn3(2, tmp_z);

		// calculate matrix for translation from old_basis to new_basis
		Mat4 translation_basis, new_transform;
		mul(translation_basis, new_basis, inverse(old_basis));

		// change node_transform by translation_basis
		mul(new_transform, translation_basis, node_transform);
		// apply node_transform
		node->setWorldTransform(new_transform);
	}
}

 

for better accurancy you need to take more than 3 points.

buoy.png

Link to comment
×
×
  • Create New...