How do I convert surface angle to number

0 favourites
  • 5 posts
From the Asset Store
296 high-quality footsteps sounds specially crafted to help you put sound to your games
  • 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.

  • 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:

    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:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

    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:

    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?

  • 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

  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)