# How do I convert surface angle to number

Get help using Construct 2

### » Tue Mar 08, 2016 12:49 pm

Is there a way to determine the angle of a surface?

Say I have a square sprite, rotated at a random angle. I know if I use the bullet behaviour, the bullet can collide with the surface and know the angle it needs to deflect off at. However, I intend on using a platform behaviour on top of this angle, so it'll recognize not only what angle it needs to stand at, but also know what angle to jump at. I don't want to it simply jump vertically straight up, but rather at the angle of the surface.

That make sense?

I've tried doing it manually by having an object check whether or not it's touching the floor with both corners and rotating itself accordingly, but it's not instant and can lead to some juddering as it tries to set itself.
B
98
S
20
G
8
Posts: 1,066
Reputation: 12,758

### » Tue Mar 08, 2016 5:01 pm

There are a couple ways to do this.

One is to take the locations of the points of the collision polygon and calculating the point of collision and angle of the surface in contact by using the Separating Axis Theorem. It's a bit more technical to do but it gives exact results:
viewtopic.php?f=147&t=127738&p=904925&hilit=wall+slide#p904925

Another simpler way is to check for collisions around an object and averaging out the offsets to get a angle. Here's one that does that when one object is a ball:
viewtopic.php?f=147&t=167241&p=1007866&hilit=bounce#p1007866
B
100
S
38
G
134
Posts: 5,556
Reputation: 85,325

### » Wed Mar 09, 2016 1:06 am

R0J0hound wrote:There are a couple ways to do this.

One is to take the locations of the points of the collision polygon and calculating the point of collision and angle of the surface in contact by using the Separating Axis Theorem. It's a bit more technical to do but it gives exact results:
viewtopic.php?f=147&t=127738&p=904925&hilit=wall+slide#p904925

Another simpler way is to check for collisions around an object and averaging out the offsets to get a angle. Here's one that does that when one object is a ball:
viewtopic.php?f=147&t=167241&p=1007866&hilit=bounce#p1007866

Sadly my collision polygons will effectively be a smoothed shape, comprising of approx 8 points.

I've attached an example of my current method. Hopefully this isn't too taxing for the cpu. The object shoots a bullet that detects the angle of the surface and feeds it into the angle of the main sprite.

However, the problem is now that when the object deflects off at (let's say) a 90 degree angle, the main sprite sets itself to 90 degrees where I'd want it to set itself to 45 degrees.

https://onedrive.live.com/redir?resid=FB21E2766119D2EA!76224&authkey=!AHhzj8Ufzmfbm3c&ithint=file%2ccapx

It's just a quick and dirty way of determining a surface angle... and apparently not a very good one.

Might you know of a way to improve on this method?
B
98
S
20
G
8
Posts: 1,066
Reputation: 12,758

### » Wed Mar 09, 2016 8:08 pm

You can still use those ideas. For instance you can take the second one and after you make sure the two objects aren't overlapping you can use overlaps at offset for a similar effect.

Using the bullet's bounce is an interesting idea, but the bounce is inconsistent.
You can however correct the angle by utilizing the equation for a bounce:
angleOut = 2*agleSurface - angleIn

So if you set rotateAngle to (Sprite3.Bullet.AngleOfMotion+90)/2-90

You can also use your original idea of using detectors at the corners. To make it instant use a loop. To make it less jittery, move them by smaller steps.

here's a wip of that I had laying around:
https://dl.dropboxusercontent.com/u/542 ... 0_wip.capx
B
100
S
38
G
134
Posts: 5,556
Reputation: 85,325

### » Wed Mar 09, 2016 11:00 pm

Unfortunately I couldn't open your capx as I'm still on the stable... but I managed to get this bullet angle idea working quite well.

270-(angle(0,0,cos(270-Self.Bullet.AngleOfMotion),sin(270-Self.Bullet.AngleOfMotion))/2)

I believe it is based on your code for cos and sine to determine the difference in angle. Then I divide it by 2, and I get the correct angle the player should be.
B
98
S
20
G
8
Posts: 1,066
Reputation: 12,758