Maths Help Please

0 favourites
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi everyone, I have a maths problem that I'm struggling with, and I was hoping someone could help.

    I'm doing a top-down game, and I would like an arrow to point to the next objective if that objective is off-screen. I have set this up no problem - when the objective item is not on screen then I am:

    * setting the arrow's poition to the the player's poition,

    * setting the arrow's angle towards the objective,

    * moving the arrow 200 pixels forward at its current angle.

    This works OK, but rather than move the arrow 200 pixels, ideally I would like to figure out the distance between the arrow and the edge of the screen, and move it forward by that number of pixels. That way the arrow would stick to the edge of the screen, if you see what I mean.

    So I know the X & Y of the viewport, and the angle the arrow is pointing, but can't get the next step. I'm sure there's some trigonometry that could help me here, but I'm struggling. Any ideas? Thanks

  • SecondDimension

    Here is an example. Its not mine just one i picked up on the forums some time ago.

    Arrow Pointer

    hope it helps <img src="smileys/smiley1.gif" border="0" align="middle">

  • Great, yes it does help! I'd tried a heavy-handed method similar to that, but I wasn't using a variable as a distance buffer; this way is definitely cleaner. Thanks

  • spongehammer

    Any chance you still have this example handy? Im trying to do something similar but for an enemy marker. I can get it working no problem as long as my layout isnt rotating, just using clamping to the viewport, but as soon as my layer rotates, the X,Y gets all bungled with the rotation so its no longer accurate. Just curious if this example tackled rotation or how i might be able to do that?

    Huge love...

  • Oh, and SecondDimension

  • I think this is the one i posted..

  • spongehammer

    Thank you so much - can you check this updated CAPX?

    Basically if you use the A & D keys, it rotates the layer by 15 degrees each way so you can see how the indicator no longer lines up to the edge of the layout. Im trying to account for the rotation of the layout and reposition it according to where it should be with the angle change, but i cant seem to figure it out properly...

    Do you have any ideas? Thank you a million times over for checking it out..

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It might be a while before i can take a look as i am away at the moment and cant access C2. You might want to look at the CanvasToLayer expressions to convert the co-ordinates as the layer is rotated.

    https://www.scirra.com/manual/126/system-expressions

  • spongehammer - Yea been messing with this trying to get it working but cant quite seem to wrap my head around the context. eg. am i clamping the arrows first, then translating it to canvas then dropping it back down on, eg .the hud layer? just cant seem to understand what i need to do exactly haha

    Also worried about performance of doing that layer/canvas translating as someone mentioned it slowed things down for them, but i will probably only have about 20-50 objects on the screen max that are being positioned in this way..

  • SecondDimension ,

    This is a simple way to do it.

    DON'T rotate the layer, on which the player or Arrow or whatever is this way, since the rotation is not centered. It confuses the coordinates.

    Rather rotate everything around the player in the center of the map.

    You can also let the arrow easily disappear, if target "is on-screen", or when the distance (rather of the XY coordinates, as done in the file, not the function dist(), as this would detect a circle, but you have a square Screen) of the halfway point being smaller than your View-Port-edges.

    Hope it helps...

  • This already puts the arrow always halfway between the player and it's target, rotating towards it.

    Its not complicated math, just a bit of XY-Coordinates adding subtracting... easy, but as you can see, very effective. And saves you a lot of performance

  • > SecondDimension ,

    This is a simple way to do it.

    DON'T rotate the layer, on which the player or Arrow or whatever is this way, since the rotation is not centered. It confuses the coordinates.

    Rather rotate everything around the player in the center of the map.

    You can also let the arrow easily disappear, if target "is on-screen", or when the distance (rather of the XY coordinates, as done in the file, not the function dist(), as this would detect a circle, but you have a square Screen) of the halfway point being smaller than your View-Port-edges.

    Hope it helps...

    Hey man, thanks so much for your time on this - Unfortunately rotating the layer is something I have to do. 14 months of development already built around the layers rotating, it would be too time consuming to switch it so everything rotates around the player.

    Also, in regards to the code above, its nice for sure, i like how it smoothly transitions from the object to the screen edge, but yeah without the rotation i just cant use it.

    i was trying to account for the rotation using transform/rotate trigonometry functions but i barely knew what I was doing, so it ended up messed up.

    p = ViewportRight/2

    q = ViewportBottom/2

    (so now p,q is the exact centrepoint of the viewport)

    newx=(x-p)*cos(?)-(y-q)*sin(?)+p

    newy=(x-p)*sin(?)+(y-q)*cos(?)+q

  • SecondDimension ,

    The map rotation problem was something I realized at the end, so I cheated deleting that possibility

    (Since I had two separate layers apparently that is, and I did not have time to realize that.)

    Now I had time to take a look again, and I revise!!

    IT DOES WORK WITH ROTATION, but all objects obviously have to be on that layer (which you want to rotate).

  • [quote:2nie8l7k]p = ViewportRight/2

    q = ViewportBottom/2

    (so now p,q is the exact centrepoint of the viewport)

    There is a mistake, Viewport gives you the COORDINATES, not the window size. Confusion! Take care, you have to use either WindowHeight/Width for that, or just use Player.X Player.Y, since he is centered! Or (ViewportBottom-ViewportTop)/2 to subtract the coordinates, and get half of the screen size. I hope you understand.

    That is, why you have to tell him, which layers coordinates to get by VieportBottom(Player.LayerNumber) or just ViewportBottom(0).

    When you work with stuff like that, it is a helpful thing to display those variables on your screen (set Text = ViewportLeft ), to avoid such confusions. Also there you get a good idea, how they relate, and how you can use that to determine the formula.

  • > SecondDimension ,

    The map rotation problem was something I realized at the end, so I cheated deleting that possibility

    (Since I had two separate layers apparently that is, and I did not have time to realize that.)

    Now I had time to take a look again, and I revise!!

    IT DOES WORK WITH ROTATION, but all objects obviously have to be on that layer (which you want to rotate).

    Well it works with rotation, but it has the same issue as my original version: when the screen is rotated, the arrow does not stay visible when you get too far from the Target... the arrow moves outside of the visible area unless you are actually near the target / the target is on-screen.

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