C2 Math question

0 favourites
From the Asset Store
No Ad-Sharing, Fully Open Source, No Remote Control, Well maintained
  • Hello, I have a math question for you ;)

    I have a rectangle within an object (so that the width/height of the rectangle are defined by object.width * k, object. width * k where k < 1)

    I want to be able to pick a random position within the rectangle.

    However, the object could be at any angle.

    (the origin of the object is the centre point of the rectangle.)

  • rectangle.x + int(random(-rectangle.width/2,rectangle.width/2))

    rectangle.y + int(random(-rectangle.height/2,rectangle.height/2))

  • this is what I normally do. The trick lies in the fact that the rectangle can be at any angle.

  • Well haven't tested, but I would try

    rectangle.x+ cos(rectangle.angle)*int(random(-rectangle.width/2,rectangle.width/2))

    rectangle.y+ sin(rectangle.angle)*int(random(-rectangle.height/2,rectangle.height/2))

  • Hmm, newt, that doesn't seem to work...

  • newt's idea is to sample a value in an unrotated rectangle and then rotate the values arround the center.

    Let see then :

    sample values inside a rectangle :

    x = rectangle.x+random(-rectangle.Width/2,rectangle.Width/2)
    y = rectangle.y+random(-rectangle.Height/2,rectangle.Height/2)

    Then you rotate the value around the center

    xRot = rectangle.x + cos(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)
    yRot = rectangle.y + sin(angle(rectangle.x,rectangle.y,xSample,ySample)+rectangle.angle)*distance(rectangle.x,rectangle.y,xSample,ySample)

    We can simplify that by sampling the rectangle at 0,0 and then offsetting the restult

    x0 = random(-rectangle.Width/2,rectangle.Width/2)
    y0 = random(-rectangle.Height/2,rectangle.Height/2)

    And then

    xRot = rectangle.x + cos(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)
    yRot = rectangle.y + sin(angle(0,0,x0Sample,y0Sample)+rectangle.angle)*distance(0,0,x0Sample,y0Sample)

    So basically, sampling a random number in a rectangle is the same as chosing a random angle and distance from the center of this rectangle and applying a rotation.

    In short it will be something like that:

    +On what you want
      Local number randX=0  //random X position withing the rectangle
      Local number randY=0  //random Y position withing the rectangle
      Local number randA=0  //random angle before rotation
      Local number randD=0  //random distance
      -> System: set randX to  random(-rectangle.Width/2,rectangle.Width/2)
      -> System: set randY to  random(-rectangle.Height/2,rectangle.Height/2)
      -> System: set randA to angle(0,0,randX,randY)
      -> System: set randD to distance(0,0,randX,randY)
      -> System: set randX to rectangle.x + cos(randA + rectangle.angle) * randD
      -> System: set randY to rectangle.y + sin(randA + rectangle.angle) * randD

    Should work...

  • Ah, so its get the rectangle's new angle, generate a random angle, get the difference between those, then generate an offset that is the same for both x, and y.

  • newt

    well, it's really generate a random point and then take it's angle AND distance from the origin.

    It's not just a random angle

    it's not just a random distance frome the origin

    it's not event a random angle and a random distance, 'cause you have to stay within the rectangle boundaries. If you take a random angle and then a random distance from the origin (from 0 to how much?), you can't be sure you are within the rectangle. Angle and distance are linked to discribe a specific point.

    So it's really a random point you convert into distance and angle from origin -> rotate -> translate.

  • Humm, it's working, however at an entirely different point on the map.

    Probably my fault, not Yann's ;)

    I do rotate layers, but I doubt that really makes much difference.

  • Be carefull, the layerangle is a bit tricky. I would advise to not use it, especially if you use scrolling. It behave pretty weirdly.

    I think that Ash did say somewhere that it's not the layer that rotate but something like, rotate layout -> paste Layer into main canvas -> rotate back. So event layerangle is reversed...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well it has worked perfectly for me in the past. Also, it is in the same wrong relative position regardless of the layer rotation.

  • Here's a different way to look at it.


    As to the layer angle, remember the rotation is counter clockwise, since in essence you are rotating the view.

  • newt

    That looks great! although why do you use numbers 4 and 16 instead of expressions of the size of the box? Basically to adapt this for my own purposes, I need to know how to adjust these constants.

  • It just divides the rectangle up into the size of the second sprite.

    Looks a little better imho, and its easier to get around the issue with hotspots on the second object.

    So rectangle width divided by the second objects width/2 *the second objects width.

  • By using a random angle and random distance the point ends up in a circle, the corners are never selected.

    Here is how to have the point random out of the whole area.


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