0 Favourites

Move to random point

  • What is the best way to move a sprite to a random point within line of sight. Say my sprite has a line of sight that is 100px. How can I pick a random x,y that is a random angle, and then move the spite random(1, 100) PX to a point on the line.

    ugh, math is confusing.

    Basically the sprite can move to one point in any direction, but only up to 100px

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • You can use polar cordinates:


    You generate an angle -> ANGLE = random(360)

    and a distance -> DIST = random(100)

    The new destination position of your sprite will be:

    x = current.X + cos(ANGLE) * DIST

    y = current.Y + sin(ANGLE) * DIS

    Or it can be simplier if the distance can be in manhattan distance, you can simply use something like this:

    x = current.X + random(200) - 100

    y = current.Y + random(200) - 100

  • condition : set angle x Degrees

    ---repeat 10 times : wait loopindex * 0.1

    : move forward 10pix

    edit - capx

    https://www.dropbox.com/s/qv8qktqunmeod ... .capx?dl=0

  • Borgi, I tried your formula like this:

    mouseX = enemyUnit.X + cos(random(360)) * random(100)

    mouseY = enemyUnit.Y + cos(random(360)) * random(100)

    Then move towards the location like this:

    set x = lerp(enemyUnit.X, enemyUnit.mouseX, 1 - 0.5 ^ dt)

    set y = lerp(enemyUnit.Y, enemyUnit.mouseY, 1 - 0.5 ^ dt)

    The problem is the each turn the unit seems to move less and less distance, and usually in the same direction. Is there something that would trip this method up? Should I be setting mouseX and mouseY to zero at the end of each turn.

    The game goes player turn move up to 100 px if there is a target in site, fire up to 120px. Same for the enemy unit.

    I know this isn't goo ai at all, but I'm just learning some basic concepts. Knowing how to move set pixels in a random direction is good . I also have to study math.

    I'm gonna check out RamPack's capx now. That seems like a simple and interesting method..

  • Check your mouseY -> that cos sohuld be sin there!

    It sohould not move less and less.

    If you can not find a souliton share your capx and we will heve a look

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