How to get mouse click coordinates on TModel3D


EDIT: updated the code and added better images.

I have a TModel3D inside a TDummy inside a TViewport3D on a frame and I need to create a sphere when I click on it.

The OnClick event does not have coordinates as parameters, so I tried with the OnMouseDown event.

procedure TMyFrame.Model3D1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Single; RayPos,
RayDir: TVector3D);
MySphere: TSphere;
HitPos: TPoint3D;

MySphere := TSphere.Create( Viewport3D1 );
MySphere.Parent := Viewport3D1;

MySphere.Width := 0.5;
MySphere.Height := 0.5;
MySphere.Depth := 0.5;

Model3D1.RayCastIntersect( RayPos.ToPoint3D, RayDir.ToPoint3D, HitPos );

MySphere.Position.X := HitPos.X;
MySphere.Position.Y := HitPos.Y;
MySphere.Position.Z := HitPos.Z;


This is the model I load to the TModel3D

The OnMouseDown evet is fired only when I click on the model, if I click on the TModel3D but not on the model the event is not fired.

And here’s my problem, after clicking on some parts of the model, the spheres are created on the borders of the TModel3D instead of on the model. Here’s 2 examples:

If I click on the knee from the front, the TSphere is created here instead of “on” the knee.

And here’s what it looks like after some click from the front, back, side, and upper views.

Here’s what it should looks like when I click on the knee

Probably because of

Model3D1.RayCastIntersect( RayPos.ToPoint3D, RayDir.ToPoint3D, HitPos );

I’ve tried searching on google but I couldn’t find anything like this, apart from this but it’s on a TPlane.

Is it possible to get x,y,z when I click on the model with OnMouseDown?
Or is it only the way I’m doing it that is wrong?

I’m using Delphi XE8 with Firemonkey

Comments are closed.