How can I determine if actor1 (circle) is completely inside actor2 (square/box)

thatmanthatman Member Posts: 7
edited November -1 in Working with GS (Mac)
I have 2 actors:
Actor1 = "circle"
Actor2 = "square"

The first level I'm trying to make has a large square and small circle. Levels will get progressively harder where the square gets smaller.

How can I determine if the circle is completely inside the square? Just touching the edge of the square (a normal "collision") is not enough -- the user needs to get the ball completely inside the square.

Thanks!

Comments

  • xarmianxarmian Member Posts: 124
    thatman said:
    I have 2 actors:
    Actor1 = "circle"
    Actor2 = "square"

    The first level I'm trying to make has a large square and small circle. Levels will get progressively harder where the square gets smaller.

    How can I determine if the circle is completely inside the square? Just touching the edge of the square (a normal "collision") is not enough -- the user needs to get the ball completely inside the square.

    Thanks!

    This will take a bit of math, and I would not put it in a collision.. we know the size of the circle and size of the square. We also know the position of the circle and square by their x and y coordinates. Make the size of the square and x/y values of the square into game attributes so we can access those values from within the circle.

    Now in the circle's behaviors we do some checking:

    if abs(circleX-squareX) <= (abs(squareSize-circleSize)/2)
    and
    if abs(circleY-squareY) <= (abs(squareSize-circleSize)/2)
    ...
    do something

    Now gamesalad makes this extra difficult because we can only compare an attribute to an expression, we can't compare two expressions. So we would make two attributes in the circle actor:

    distanceDiffX (double)
    distanceDiffY(double)

    now, in the behaviors for the circle, add two rules:

    constrain self.distanceDiffX to abs(self.Position.X-game.squareX)
    constrain self.distanceDiffY to abs(self.Position.Y-game.squareY)

    and you would now just need to compare those attributes to the sizes of the square and circle. (self.width and game.squareSize above)

    I think this is accurate, but I haven't tested the math, so there could be a flaw to my logic. I'll let someone correct me if I've made a mistake, but the general concept should work.

    Dave
  • GLGAMESGLGAMES SingaporeMember Posts: 988
    wow that is some formula.
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    Here's what I would do to avoid using constrain (assuming that the square is not moving). I would create two integer attributes if it is a rectangle and only one if it always going to be a perfect square (call it game.SquareSize) for the size. Then I would create two more game integer attributes for position (game.SquareX and game.squareY). Oh, and create game.gameover (boolean)

    In the square actor:

    change attribute game.squareSize to self.size.width
    change attribute game.squareY to self.position.Y
    change attribute game.squareX to self.posiiton.X

    In the circle.

    Rule (Any): if self.position.X < (game.SquareX-((.5*game.SquareSize)+(.5*self.size.width)))
    if self.position.X > (game.SquareX+((.5*game.SquareSize)+(.5*self.size.width)))
    if self.position.Y > (game.SquareY+((.5*game.SquareSize)+(.5*self.size.height)))
    if self.position.X < (game.SquareY-((.5*game.SquareSize)+(.5*self.size.height)))

    change game.gameover to 1

    Then wrap all your movement rules in a rule that says "when game.gameover is false"
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    If it was a circle within a circle you could easily use magnitude to do this. It would be a single line of code.
  • LumpAppsLumpApps Member Posts: 2,881
    Same for a square in a circle if I'm not mistaken.
  • thatmanthatman Member Posts: 7
    could you please elaborate on how to use magnitude, if i was to change the square target to a circle
  • thatmanthatman Member Posts: 7
    scitunes said:
    Here's what I would do to avoid using constrain (assuming that the square is not moving). I would create two integer attributes if it is a rectangle and only one if it always going to be a perfect square (call it game.SquareSize) for the size. Then I would create two more game integer attributes for position (game.SquareX and game.squareY). Oh, and create game.gameover (boolean)

    In the square actor:

    change attribute game.squareSize to self.size.width
    change attribute game.squareY to self.position.Y
    change attribute game.squareX to self.posiiton.X

    In the circle.

    Rule (Any): if self.position.X < (game.SquareX-((.5*game.SquareSize)+(.5*self.size.width)))
    if self.position.X > (game.SquareX+((.5*game.SquareSize)+(.5*self.size.width)))
    if self.position.Y > (game.SquareY+((.5*game.SquareSize)+(.5*self.size.height)))
    if self.position.X < (game.SquareY-((.5*game.SquareSize)+(.5*self.size.height)))

    change game.gameover to 1

    Then wrap all your movement rules in a rule that says "when game.gameover is false"

    this should be rule(all), right?
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    no. if you have it set to all it would mean that you would have to be above and below and left and right of the square all at the same time. Currently in this universe that is impossible (unless you are QS)

    :)

    If you have it set to "any" it should work.
  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    magnitude finds the distance between two points

    so you could have

    magnitude(abs(smallCircle.X-target.X), abs(smallCircle.Y-target.Y))

    this will give you the positive (absolute value) distance between the center point of the small circle, and the center point of the target.

    so if you're targer has a diameter of 100px, and the small circle has a diameter of 40. you want the magnitude to be less than 30px. (100px would be a 50px radius)
  • thatmanthatman Member Posts: 7
    thanks everyone... here's an embarrassing question...

    im using the "angry birds" example template for to create the motion... so smallCircle has a velocity...

    what's the most efficient way to check it's current position when using any of the solutions here? not sure how to effectively check the rule set that scitunes explained above
Sign In or Register to comment.