[Plugin] jcw_trace (raycast)

Post your work in progress addons and get feedback

Post » Wed May 11, 2016 2:53 am

Nice plugin. Here's a quick thought on the normal being wrong on flipped tiles. Normally the collision polygon have it's points ordered in the same way (cw or ccw). This is called winding. So when a ray hits an edge you can take the angle from one point of the segment to the other, and if the points have clockwise winding you can subtract 90 and get the normal.

Mirroring will switch the winding and so will mirroring. So if something is both flipped and mirrored the winding will be the same as they are originally. Negative sizes of sprites have the same affect as flipping and mirroring.


Roughly it would look like this, it might need to be swapped:

If mirrored xor flipped
EdgeAngle+90
Else
EdgeAngle-90

That can fail though if the user creates a collision polygon with a reversed winding. It's not common but it has happened before. I didn't handle it and instead had the user correct his collision polygon, but you can handle it in code by either checking the winding of the polygon or sampling a point on one side of the edge to know which side is inside the polygon.

As far as speed goes, the best improvement would be to do some kind of spacial partitioning of the objects you cast to. You can probably simplify the math too in areas but that may not be as significant of an improvement.

Ps.
You can also calculate the reflection angle without vector math.
ReflectAngle = 2*normal-rayAngle
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Wed May 11, 2016 7:56 am

EpicPixel wrote:Hey, great plugin. Thanks for sharing.
I made an icon you can use if you like it. icon


the 16x16 looks nice, but the larger ones look like they have been upscaled from that smaller one, and don't look as nice.

R0J0hound wrote:Nice plugin. Here's a quick thought on the normal being wrong on flipped tiles. Normally the collision polygon have it's points ordered in the same way (cw or ccw). This is called winding. So when a ray hits an edge you can take the angle from one point of the segment to the other, and if the points have clockwise winding you can subtract 90 and get the normal.

Mirroring will switch the winding and so will mirroring. So if something is both flipped and mirrored the winding will be the same as they are originally. Negative sizes of sprites have the same affect as flipping and mirroring.


Roughly it would look like this, it might need to be swapped:

If mirrored xor flipped
EdgeAngle+90
Else
EdgeAngle-90

That can fail though if the user creates a collision polygon with a reversed winding. It's not common but it has happened before. I didn't handle it and instead had the user correct his collision polygon, but you can handle it in code by either checking the winding of the polygon or sampling a point on one side of the edge to know which side is inside the polygon.


I am already aware of what causes it, and have been considering testing which side of the line the start point is on to fix it. But thanks anyway.

R0J0hound wrote:As far as speed goes, the best improvement would be to do some kind of spacial partitioning of the objects you cast to.


Isn't this what the collision cells do?

R0J0hound wrote:Ps.
You can also calculate the reflection angle without vector math.
ReflectAngle = 2*normal-rayAngle


Thanks! I'm gonna use this now.
B
54
S
19
G
13
Posts: 97
Reputation: 10,146

Post » Wed May 11, 2016 8:23 am

Collision cells are a form of spatial partitioning. As I recall the cells are screen sized, so even when using them, your examples would still have to loop over all the objects.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Wed May 11, 2016 8:29 am

I've updated the plugin again.

  • Added padding property, to set how much space is left between the hit object and the result position
  • Added 'Hit' condition which returns true if the last trace performed hit something.
  • Fixed normal direction on flipped tiles and other polygons with alternate winding.
  • Now using a simpler calculation for reflection.

The example now has an absurd reflection test on the tilemap layout.

I'm planning on adding hull traces, but I'll need to do some research on the math required for that.
B
54
S
19
G
13
Posts: 97
Reputation: 10,146

Post » Wed May 11, 2016 4:09 pm

Great additions :!:
I updated the icon Icon

[ EDIT ]
Rexrainbow had problems with the icon above so here is another update :roll:
This one works for me and rexrainbow.

And I know much more about icons now. :D
Cheers
B
69
S
28
G
7
Posts: 85
Reputation: 7,521

Post » Tue May 24, 2016 8:35 am

I've updated the plugin. If a trace starts inside of an object, the hit position will now be at the start, instead of hitting the inside of the polygon. I'm not quite sure what to do with the normal in this case, so I just set it to be opposite of the trace line. I also added a HitFrac expression, which is used internally to calculate the hit position. I find it useful for checking if the trace started inside an object, but I'm sure there are other uses for it.

Not much progress on the hull trace front. Resources on the topic are hard to find.
B
54
S
19
G
13
Posts: 97
Reputation: 10,146

Post » Fri May 27, 2016 9:00 pm

Hi @Johncw87, this is an excellent plugin! I have come across one glitch, however - I am unable to export using minification - the minification fails as if java was not installed... If I remove the plugin then everything exports normally. Will you be able to add minification support at some stage? Thanks!!
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Sat May 28, 2016 1:52 am

@Colludium
Of course. The one time I didn't update the example (which used the minifier), the minifier stops working.

The fix turned out to be easy, though. I had added default parameters to a Javascript function, which is apparently not yet standardized (but supported by Firefox). It would seem that the minifier didn't like this, so I took that out.

Re-download the plugin.
B
54
S
19
G
13
Posts: 97
Reputation: 10,146

Post » Sat May 28, 2016 9:08 am

Awesome, @Johncw87! Thank you :).
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Tue Jun 07, 2016 3:09 am

Would it be possible to turn this into a behavior so different objects can store different trace results? I'm not a programmer so I have no idea how difficult that would be.
B
80
S
52
G
41
Posts: 370
Reputation: 25,830

PreviousNext

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests