Problem with TraceScreenRay

Problem with TraceScreenRay

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
very_bad_soldier
Posts: 1371
Joined: 20 Feb 2007, 01:10

Problem with TraceScreenRay

Post by very_bad_soldier » 22 Apr 2012, 07:12

I have a problem with the easyFacing widget I dont seem to understand. The widget draws an arrow that indicates the facing of the factory when you are placing them.
So I use TraceScreenRay and Pos2BuildPos to translate the current mouse cursor position to the build position.
But when you move the mouse over a feature the return value of TraceScreenRay returns the height of the feature (I think). The returned y-value is higher than before. According to my understanding the second parameter "onlyCoords" indicates that features should not be taken into account? At least when it is true all return values have the type "ground".

Here are two screenshots that show the issue. Since the ghosted factory is identical the widget is supposed to draw the arrow at the same position in both images which is not the case.
Image
Image

In the console there are the debug outputs shown. I think it has something to do with the feature that gets hit by the screen ray. The y-value of the TraceScreenRay is different then also.
In the end the z-Value of the BuildPos is different which should not be the case.

Last but not least, thats the source code in question:

Code: Select all

Spring.Echo("Mouse: X: " .. mx .. " Y: " .. my)
local type, coords = spTraceScreenRay(mx, my, true, true)
	
if not coords then return end
		
local centerX = coords[1]
local centerY = coords[2]
local centerZ = coords[3]
Spring.Echo("Trace: Type: " .. type .. " X: " .. centerX .. " Y: " .. centerY .. " Z: " .. centerZ)
	
centerX, centerY, centerZ = spPos2BuildPos( unitDefID, centerX, centerY, centerZ )

Spring.Echo("BuilPos: X: " .. centerX .. " Y: " .. centerY .. " Z: " .. centerZ)
Thanks for hints!
0 x

User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Problem with TraceScreenRay

Post by knorke » 22 Apr 2012, 11:58

But when you move the mouse over a feature the return value of TraceScreenRay returns the height of the feature (I think)
if you hover the mouse over feature/unit it gives the ID of feature/unit.

maybe get height coordinate like this?
http://springrts.com/phpbb/viewtopic.ph ... ay#p506505
0 x

User avatar
very_bad_soldier
Posts: 1371
Joined: 20 Feb 2007, 01:10

Re: Problem with TraceScreenRay

Post by very_bad_soldier » 22 Apr 2012, 12:55

knorke wrote:
But when you move the mouse over a feature the return value of TraceScreenRay returns the height of the feature (I think)
if you hover the mouse over feature/unit it gives the ID of feature/unit.
It does only do that if the parameter "onlyCoords" is false. In my case it actually returns ground coordinates. You can see that in the screenshots in the output line "Trace: type: ground".
Only If I set onlyCoords to false then I get type "feature" when hovering over a feature.
0 x

gajop
Moderator
Posts: 3023
Joined: 05 Aug 2009, 20:42

Re: Problem with TraceScreenRay

Post by gajop » 22 Apr 2012, 13:07

i'd check to see if it isn't just giving the coords of the unit/feature when onlyCoords is set to true, i.e if it isn't just doing getPosition() on the id of the traced unit/feature and returning that
0 x

User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Problem with TraceScreenRay

Post by Niobium » 24 Apr 2012, 04:31

TraceScreenRay always checks for intersections with units/features, no matter the arguments. If 'onlyCoords' is on and the ray intersects with a unit or feature, it returns the point of intersection on the collision volume (rather than the unit or feature ID).

I made a feature request some time ago, but nothing came of it: http://springrts.com/phpbb/viewtopic.ph ... 07#p454407.

Workarounds:
a) Use GetGroundHeight(x,z) instead of y value, gives ok results for low units, and bad results for high units (esp. air units)
b) Using some vector maths with the camera, make an approximation of the true intersect position by following the ray from the collision point until y has decreased by (y - getgroundheight(x,z))
c) Implement the trace-ray-to-ground code from spring in lua. I've actually done this, and its quick, but it's 100 lines long which made it too complicated to risk putting into custom formations (which is where this tracescreenray oddity is commonly problematic)
0 x

User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: Problem with TraceScreenRay

Post by jK » 24 Apr 2012, 05:41

TraceScreenRay is correct, it's Pos2BuildPos that fails (engine & lua use different rounding schemes).
0 x

User avatar
very_bad_soldier
Posts: 1371
Joined: 20 Feb 2007, 01:10

Re: Problem with TraceScreenRay

Post by very_bad_soldier » 24 Apr 2012, 10:32

I think Nio is right: you get the coordinates where the ray hits the collision volume of the feature.
But it does not help to just exchange the y-value because the x- and/or z-values are also wrong.

Look at the picture, the problem gets worse when the camera angle is changed (thats why I think the problem is the feature collision intersection):
Image

Pos2BuildPos might also have a problem but as the pictures show the error is bigger than a different rounding result would have.

So I am afraid there is no clean solution to this currently :( Would be cool to have another parameter for TraceScreenRay to make it ignore features and only intersect with ground.
0 x

User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Problem with TraceScreenRay

Post by CarRepairer » 24 Apr 2012, 11:14

If this is implemented I would like to tack on another request. When using tracescreenray on a point outside the map, return a virtual point where terrain would be if it were extended to that point.
0 x

User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6109
Joined: 29 Apr 2005, 01:14

Re: Problem with TraceScreenRay

Post by FLOZi » 24 Apr 2012, 15:02

fix LuaUnsyncedRead::TraceScreenRay when onlyCoords=true
7d95cfe9cd
Browse code
rt authored 8 hours ago
https://github.com/spring/spring/commit ... 355f65a495
0 x

User avatar
very_bad_soldier
Posts: 1371
Joined: 20 Feb 2007, 01:10

Re: Problem with TraceScreenRay

Post by very_bad_soldier » 25 Apr 2012, 11:06

Yay big thanks rt! (Kloot?)

I dont exactly understand what the new behavior will be judging from the source code. Will it ignore features when onlyCoords is set or will it return the ground coordinates of a feature which intersects the ray?
0 x

Kloot
Spring Developer
Posts: 1865
Joined: 08 Oct 2006, 16:58

Re: Problem with TraceScreenRay

Post by Kloot » 25 Apr 2012, 17:51

0 x

User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Problem with TraceScreenRay

Post by aegis » 25 Apr 2012, 18:20

CarRepairer wrote:If this is implemented I would like to tack on another request. When using tracescreenray on a point outside the map, return a virtual point where terrain would be if it were extended to that point.
I already made that patch two years ago - it's how my SmartSelect widget works outside the map.

http://springrts.com/wiki/Lua_UnsyncedRead#Screen_Trace

Use true for the includeSky parameter to make it work.

I use this to keep perfect track of the selection rectangle while you're dragging it, even if the camera/screen change angle/position during the drag. Works perfectly.

TraceScreenRay(x, y, true, false, true), then WorldToScreenCoords the result at any time to get the screen position of where you originally clicked.
0 x

User avatar
CarRepairer
Cursed Zero-K Developer
Posts: 3359
Joined: 07 Nov 2007, 21:48

Re: Problem with TraceScreenRay

Post by CarRepairer » 26 Apr 2012, 06:52

aegis wrote:
CarRepairer wrote:If this is implemented I would like to tack on another request. When using tracescreenray on a point outside the map, return a virtual point where terrain would be if it were extended to that point.
I already made that patch two years ago - it's how my SmartSelect widget works outside the map.

http://springrts.com/wiki/Lua_UnsyncedRead#Screen_Trace

Use true for the includeSky parameter to make it work.

I use this to keep perfect track of the selection rectangle while you're dragging it, even if the camera/screen change angle/position during the drag. Works perfectly.

TraceScreenRay(x, y, true, false, true), then WorldToScreenCoords the result at any time to get the screen position of where you originally clicked.
Wow I wish I'd known that earlier. I can finally improve my camera widget now.
0 x

Post Reply

Return to “Lua Scripts”