# Move to random point

Get help using Construct 2

### » Sun Aug 30, 2015 6:56 pm

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
B
7
S
2
Posts: 92
Reputation: 663

### » Sun Aug 30, 2015 7:06 pm

You can use polar cordinates:
https://en.wikipedia.org/wiki/Polar_coordinate_system

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
B
10
S
2
G
1
Posts: 51
Reputation: 761

### » Sun Aug 30, 2015 7:08 pm

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
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,902

### » Sun Aug 30, 2015 8:28 pm

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..
B
7
S
2
Posts: 92
Reputation: 663

### » Sun Aug 30, 2015 9:08 pm

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
B
10
S
2
G
1
Posts: 51
Reputation: 761