Pinning to rotated rectangle area

  • Hi there,

    I have a spite that is acting like a bucket of sorts. It is rectangular and starts off portrait in orientation. I can easily pin objects to this area randomly scattered around its surface *but*, if I rotate the sprite 90% clockwise that doesn't take effect when pinning. It basically pins to the previous portrait orientation which is effectively now hanging off the edges of my sprite.

    Is there a way I can pin to this rectangle regardless of its rotation? Many thanks in advance.

    Nick.

    Correct

    Incorrect

  • I should add that they rotate with the sprite just fine, keeping their location within the container, it's just when adding them to the container it only works correctly when the sprite is portrait in orientation.

    This is my event

  • It's because you are using the height and width of the object in the code ( the first line. ) Even when the bucket is sideways, you still use x and y coordinates and add random amounts related to it's standing position height and width.

    You can give the bucket object an instance variable called "IsStanding" then;

    on touched bucket

    if bucket is standing

    {use the code in the screenshot you shared }

    else

    {replace the formula: ((TrollySpace.X - TrollySpace.Height /2) + random(trollySpace.Height) , (TrollySpace.Y-TrollySpace.Width/2) + random(TrollySpace.Width))}

    I hope I could describe it

  • Hi there,

    thanks for the speedy response! I've tried as you suggested and that makes it work for horizontal also, but the only problem is that it means the sprite has to be in either vertical or horizontal orientation, it's a car sprite and can rotate 360 degrees.

    Sometimes the sprite is at 45 degrees or some other arbitrary angle.

  • I think I'm going to try rotating the x and y coordinates manually to put them within the rectangle at any angle, I'll update with my findings.

  • I have managed to get a single algorith working for 90 degree angles, But for some reason it's not working with anything inbetween,

    X =

    (((((PinArea.X -PinArea.Width / 2) + random(PinArea.Width)) - PinArea.X) * cos(PinArea.Angle) - (((PinArea.Y -PinArea.Height / 2) + random(PinArea.Height)) - PinArea.Y) * sin(PinArea.Angle)) + PinArea.X)

    Y =

    (((((PinArea.X -PinArea.Width / 2) + random(PinArea.Width)) - PinArea.X) * sin(PinArea.Angle) + (((PinArea.Y -PinArea.Height / 2) + random(PinArea.Height)) - PinArea.Y) * cos(PinArea.Angle)) + PinArea.Y)

  • Yeah you are on the right track. But when it comes to math, I am fairly limited myslef. Try changing places of sin and cos... That's what I do and pray it works

  • Okay so here's the issue, firstly cos and sin expect the value in radians not degrees. So to convert to radians from degrees you do this,

    pi * angle / 180

    Although oddly when I try this in c# compared to construct 2 I get,

    C#

    radians =1.5707963267949

    cos = 6.12303176911189E-17 - sin = 1

    Construct 2

    radians =1.5707963267949

    cos = 0.9996242169 - sin = 0.0274121336

    also the bog standard windows calculator gives the same answer as construct 2. Weird?!

  • Okay finally got it, much simplified using functions now,

    RotatePointX (object x, object y, area x, area y, angle)

    ((Function.Param(0) - Function.Param(2)) * cos(Function.Param(4)) - (Function.Param(1) - Function.Param(3)) * sin(Function.Param(4)) + Function.Param(2))

    RotatePointY (object x, object y, area x, area y, angle)

    ((Function.Param(0) - Function.Param(2)) * sin(Function.Param(4)) + (Function.Param(1) - Function.Param(3)) * cos(Function.Param(4)) + Function.Param(3))

    then to use it,

    Call set position on the object

    x = Function.Call("RotatePointX", PinObject.X, PinObject.Y, PinArea.X, PinArea.Y, PinArea.Angle)

    y = Function.Call("RotatePointY", PinObject.X, PinObject.Y, PinArea.X, PinArea.Y, PinArea.Angle)

  • Here is the capx file

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • okay maybe not it won't let me...

  • I've attached the solution capx to this post.

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