No ray cast responce (SOLVED)

I am trying to make ray cast for box end points.I should always have 4 responce. But sometimes i do not have ray_cast_responce where responce should exists.It may be rounding error or simething like it, but look at screenshot 4.Where is top left ray cast.

Hmm, it’s hard to say just by looking at the screenshot. Perhaps you can post some of the relevant code for generating the raycasts? I wonder if the corner actually is where we think it is?

When debugging, I usually also draw the actual raycast (not the response), just slightly offset, so I can see where it was intended to go.

1 Like

Code:

go.property("rays", 30)
function init(self)
	self.updates=0
	self.my_interesting_groups = {hash("block")}
	self.collisions={}
	self.collisions_polygons={}
end


function update(self, dt)
	log(self,"update")
	self.updates=self.updates+1
	update_collisions(self)
end


function on_message(self, message_id, message, sender)
    if message_id == hash("ray_cast_response") then
    	log(self,"ray cast responce " .. message.position)
    	msg.post("@render:", "draw_line", { start_point = go.get_position(), end_point = message.position, color = vmath.vector4(1, 1, 1, 1)})
    end
    if message_id == hash("contact_point_response") then
    	log(self,"contact responce " .. message.other_id)
    	table.insert(self.collisions,message.other_id)
  end
end

function on_input(self, action_id, action)
    
end

function log(self,message)
	if(self.updates<300)then 
		print(message) 
	end
end

function update_collisions(self)
	local collisions_polygons={}
	for i, collision in ipairs(self.collisions) do
  		local polygon = get_polygon_data(self,collision)
  		ray_casts_to_polygon(self,polygon)
  		log(self,"ray_cast " .. i)
	end
	self.collisions={}
end

function ray_casts_to_polygon(self,polygon)
	for i,point in ipairs(polygon["polygon"]) do
		log(self,point)
		physics.ray_cast(go.get_position(), point, self.my_interesting_groups)
		msg.post("@render:", "draw_line", { start_point = go.get_position(), end_point = point, color = vmath.vector4(1, 0, 0, 1)})
	end
end

function get_polygon_data(self,id)
	local polygon_data={}
	--polygon, rays
	log(self,id)
	local url = msg.url(id)
	url.fragment = "script"
	local point1= go.get_position(url)+go.get(url,"point1")
	local point2= go.get_position(url)+go.get(url,"point2")
	local point3= go.get_position(url)+go.get(url,"point3")
	local point4= go.get_position(url)+go.get(url,"point4")
	polygon_data.polygon={point1,point2,point3,point4}
	return polygon_data
end

I add ray cast lines

go.get(url,“point1”) point that hardcode for wall object.For all objects points is the same

Hmm, come to think of it, I think you are right in the fact that it might be a floating point precision thing. In general, I wouldn’t expect to get reliable results by ray casting towards a point, from a slight angle.
Perhaps you can try to “shrink” your points by some margin? I.e. move them inwards by some threshold value? Does it work then?

On another note, for functions you add in your script, make a habit of tagging them “local”. Otherwise they’ll become global and you might get weird behavior later on (i.e. when you have several instances running the same script)

2 Likes

thx,if i add small margin everything work.

2 Likes

Hi I think I am passing the same issue, could you tell me how you solved it?

The problem with float precesion.

2 Likes

Yes, indeed it worked using that approach.