Detecting sides of a rectangular sprite

Get help using Construct 2

Post » Sat Sep 27, 2014 2:20 pm

What I'm trying to do is work out which side of an arbitrarily rotated rectangular sprite is hit, so I can then get a reflection angle for the Light behavior plugin.

I can already do this for sprites which stay at 0 degrees, as you can see here:

https://dl.dropboxusercontent.com/u/52354372/detect%20rectangular%20sprite%20side.capx

That's basic trig comparing the angle between sprite & hit point with 4 ranges for each side.

The problem I'm having is working out the math for a rotated sprite. Any idea how to do this?
B
27
S
12
G
1
Posts: 157
Reputation: 3,359

Post » Sat Sep 27, 2014 6:29 pm

You just need to subtract the angle of the sprite from the angle you're already calculating. Since Angle() returns a value between -180 and 180 while the angle of a sprite is between 0 and 360, you'll need to adjust for this by adding 360 degrees to your calculated angle if it is less than -180 so that your final value is also between -180 and 180. Here's a one line solution(using the conditional operator):

Set ang to (angle(Sprite.X,Sprite.Y,hit.X,hit.Y) - Sprite.Angle) < -180 ? (angle(Sprite.X, Sprite.Y, hit.X, hit.Y) - Sprite.Angle + 360) : (angle(Sprite.X,Sprite.Y,hit.X,hit.Y) - Sprite.Angle)
Moderator
B
113
S
41
G
20
Posts: 1,586
Reputation: 19,116

Post » Sun Sep 28, 2014 1:11 pm

Well that was embarrasingly simple now you've explained it :) Thanks! Also thanks for showing the '?' operator, hadn't seen that before.
B
27
S
12
G
1
Posts: 157
Reputation: 3,359


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, GrogIsGood, plinkie, rayolf and 7 guests