Archive 17/01/2023.

[SOLVED] 2D raycast

Mike

Currently I’m using this piece of code (inside a MouseButtonDown Event) to acquire a RigidBody2D (in lua):

	local touchPos = camera:ScreenToWorldPoint(Vector3(eventData:GetInt("X") / graphics.width, eventData:GetInt("Y") / graphics.height, 0))
	local body = physicsWorld2D:GetRigidBody(touchPos)

It works fine, but is there a better/easier way to do this?

aster2013

Hi, mike

Do you think provide a function

RigidBody2D* PhysicsWorld2D::GetRigidBody(int mouseX, int mouseY, Camera* camera = 0) const; is better?

Mike

Hi aster,

This kind of function would be awesome as it does the math for us and is easy to use (less script is always better).

aster2013

I have added it for you, but please attention, when your viewport size not equal to graphics size, it will get error result.

Mike

That’s very nice, thanks aster.

Mike

Aster, sorry for not having seen this at first, but ‘mouseX’ and ‘mouseY’ can in fact be misleading, as the function is intended to work with any kind of input, whether mouse, touch, joystick…
inputX and inputY, or simply x and y as in GetElementAt() could be more explanatory, what do you think?

aster2013

thanks, i will rename it.