Jump to content

Using ray to detect objects


photo

Recommended Posts

Good evening,

I'm using the code below, but when the spheres are created they are not in the center of the mouse which means the code won't cast rays to the correct cursor point vec3:

  public PlayerSpectator cam;
  public ObjectMeshDynamic createSphere(float radius,vec3 position,int i){
	ObjectMeshDynamic sphere = Primitives.CreateSphere(radius, 9, 32);
	sphere.TriggerInteractionEnabled = true;
	sphere.SetIntersection(true, 0);
	sphere.SetIntersectionMask(0x00000080, 0); // check the BulletIntersection bit
	sphere.WorldPosition = position;
	sphere.Name = $"{i}";
	return sphere;
 }

  private void Init(){
	ControlsApp.MouseHandle = Input.MOUSE_HANDLE.USER;
 }

  void Update(){
	if (Input.IsMouseButtonDown(Input.MOUSE_BUTTON.LEFT)){
		ivec2 mainWindow = WindowManager.MainWindow.Size;
		int mouseX = Gui.GetCurrent().MouseX;
		int mouseY = Gui.GetCurrent().MouseY;
		vec3 cursorDirection = new vec3(cam.GetDirectionFromScreen(mouseX, mouseY, 0, 0, mainWindow.x, mainWindow.y)) * 50;
		createSphere(1f,cam.WorldPosition + cursorDirection,1);
	}
  }

How can I center the created spheres to the mouse cursor?

Edited by Sevdat
Link to comment

Try to use getDirectionFromMainWindow instead. Probably it would provide better results:

Math::ivec2 mouse = InputMousePosition();
vec3 cursorDirection = new vec3(cam.getDirectionFromMainWindow(mouse.x, mouse.y) * 50;

 

  • Like 1

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

Link to comment
1 hour ago, silent said:

Try to use getDirectionFromMainWindow instead. Probably it would provide better results:

Math::ivec2 mouse = Input::getMousePosition();
vec3 cursorDirection = new vec3(cam.getDirectionFromMainWindow(mouse.x, mouse.y) * 50;

 

Good day silent,

You are a genius. The code works perfectly:

 ivec2 mouse = Input.MousePosition;
 vec3 cursorDirection = new vec3(cam.GetDirectionFromMainWindow(mouse.x, mouse.y)) * 50;

but I think in C# getMousePosition is MousePosition which is strange. I think naming C++ and C# functions the same would be better for future development.

https://developer.unigine.com/en/docs/future/api/library/controls/class.input?rlang=cpp

Link to comment
×
×
  • Create New...