# SOLVED Need help with AI of pirate ships

Get help using Construct 2

### » Fri Jan 11, 2013 2:19 pm

If player is too far nothing happen but if is too close to oponent, oponetn will automatically turn to right side like ships on sea fights
B
119
S
32
G
51
Posts: 1,896
Reputation: 38,574

### » Fri Jan 11, 2013 2:28 pm

If distance( Pirate.X, Pirate.Y, Player.X, Player.Y ) < 50
| If angle( Pirate.X, Pirate.Y, Player.X, Player.Y ) < 0
| If angle( Pirate.X, Pirate.Y, Player.X, Player.Y ) > 180
| Pirate rotate to the left
|
| If angle( Pirate.X, Pirate.Y, Player.X, Player.Y ) < 180
| If angle( Pirate.X, Pirate.Y, Player.X, Player.Y ) > 0
| Pirate rotate to the right

| = "if distance" event block.

You'll need to adjust the angle tests if you want it within a certain range.
B
15
S
5
G
7
Posts: 876
Reputation: 5,650

### » Fri Jan 11, 2013 3:12 pm

@procrastinator is this is right? because nothing happens with pirate ships
B
119
S
32
G
51
Posts: 1,896
Reputation: 38,574

### » Fri Jan 11, 2013 4:35 pm

That's right, but I think my logic is flawed. I didn't take into account the angle the player may be facing. Give me a short while. Brain fog ;)
B
15
S
5
G
7
Posts: 876
Reputation: 5,650

### » Fri Jan 11, 2013 5:13 pm

sure, well i add rotate with clockwise but this is not good solution because ship will be rotated over and over without smooth rotate. I think this part is dificult to do
B
119
S
32
G
51
Posts: 1,896
Reputation: 38,574

### » Fri Jan 11, 2013 5:48 pm

I don't know about you guys, but I like to be lazy. Instead of distance checks. I like to use invisible sprites(visible with low opacity for testing) that are pinned to the object(also with the new container object).

That way you can create your collision polygon either by setting the collision points or creating an image shape to match your above example.
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,167

### » Fri Jan 11, 2013 5:56 pm

@jayderyu we got already check system but how to set auto rotate ship
B
119
S
32
G
51
Posts: 1,896
Reputation: 38,574

### » Fri Jan 11, 2013 6:09 pm

[QUOTE=jayderyu] I don't know about you guys, but I like to be lazy. Instead of distance checks. I like to use invisible sprites(visible with low opacity for testing) that are pinned to the object(also with the new container object).

That way you can create your collision polygon either by setting the collision points or creating an image shape to match your above example.[/QUOTE]

Yeah I only just thought of that and came back to mention it. You beat me to it ;p

@delgado, I just thought... if it's a pirate ship, wouldn't it fire from the sides? I mean, they'd do the opposite to what you're actually asking - they'd rotate so their sides are facing the player since the sides are where the cannons are.. right?

Anyway, you can use
PirateShip.Rotate To Position (Player.X, Player.Y)
to turn the ship to the player's position.
B
15
S
5
G
7
Posts: 876
Reputation: 5,650

### » Fri Jan 11, 2013 6:22 pm

@procrastinator yes pirate ship have to turn sideways to the player Because cannons are on left and right sides.delgado2013-01-11 18:37:26
B
119
S
32
G
51
Posts: 1,896
Reputation: 38,574

### » Fri Jan 11, 2013 6:39 pm

part of the problem as I understand angle in C2. is that what your looking for is relative angle to the ship, but angle itself is always going to be determined based on angle that 0 is always point to the right.

If your relative angle math doesn't come out right your going to get some strange results or no result at all. This is why I suggest the easy way. It increase your object count, but it is easier to implement.

https://dl.dropbox.com/u/14087254/lazyshipturn.capx

sorry. But I spent about 20m tinkering with the method you want. I decided I didn't want to pull out the calculator and figure out relative angle trig when there was a vastly easier way to do it.

Go with which ever way you want, but don't forget that angles are -180 to +180. Where as your post only looks like it's going from 0 to 180.
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,167

Next