# How do I pathfinding, and goals

Get help using Construct 2

### » Sat Sep 12, 2015 6:58 pm

ok, bare with me on this...

i have a game where a car hits a ball, it goes into the goal, woo you score.

now i want an enemy car that pushes the same ball into your goal...How? lol

i was thinking something along the lines of work out the angle from the ball to the goal, then move the AI car behind it and then set the angle to the goal position and drive forwards... but .. rrr ... if theres any better suggestions i would appreciate the feedback. I'll edit this as i tinker
B
9
S
3
Posts: 113
Reputation: 1,009

### » Sun Sep 13, 2015 2:04 am

The idea should work. You can calculate the angle from the ball to the goal with the angle() expression and then calculate a position behind the ball to hit it to the goal with this:

This is basically all the trig you need for games. For x and y use the ball's position in this case.
So with this you'd move to that position then to the ball. The next issue to address is the car can hit the ball accidentally when moving to that position. To fix it you'd need to move left or right of the ball first.
At this point it becomes a bit tedious to describe but a picture would help a lot.

Basically
Angle2ball is from the player to the ball
Angle2goal is from the ball to the goal

If both angles are Within 90 degrees of each other then move to the point behind the ball
Otherwise
If clockwise go to left point
If counterclockwise go to the right.

I got it working in a capx but I can't post it for a day or so.
Here are the points:
Behind ball

Left point. Right can be found by making 90 positive

The radius can be whatever looks good. Also I was able to get a more pleasing result by using 135 instead of 90 for the left and right points. I apologize this doesn't cover exactly how to make it work but it should be good enough to get the idea across.
B
101
S
39
G
134
Posts: 5,591
Reputation: 85,520

### » Sun Sep 13, 2015 2:31 pm

honestly you are ahero, i gave up using sohcahtoa as i couldn't figure out how to use tan^-1 to get the angle from the sum (opposite/adjacent)
i've seen you write alot of posts, and your answers always seem to involve trig, if your the reincarnation of Pythagoras i thank you

not sure how to post pictures... so i guess i should troll the how to section before asking, If you get a capx up i'd apprieciate it. again, i'll troll how 2 so i can post a cap x, i'd love to share my fan made beta pokemon game

after this attempt, and adding some variables to decide what to do when arrived, I +90 instead of 180, it moved into position then striking the ball and scored!!. So happy days... then failed when the sprite was moved :/ its ok, good start

now back to my priority system i was failing so hard on

this really makes me appreciate fifa games, and i NEVER thought i'd say that.
B
9
S
3
Posts: 113
Reputation: 1,009

### » Sun Sep 13, 2015 2:40 pm

now i'm on a loop where he scored in my goal, then his goal...

I've tried using a sprite, with image point 0 to be set to the goal, then stretched to the balls position
then added a new sprite "target" at image point 1 (just past the balls location) but he won't go there.

I'll keep on with the trig, but my fundamental knowledge on maths in that area is a bit fuzzy.
B
9
S
3
Posts: 113
Reputation: 1,009

### » Mon Sep 14, 2015 11:43 pm

@Aher0
Here's the capx:
https://dl.dropboxusercontent.com/u/542 ... imple.capx

The motion is jarring at times but the players run back and forth trying to kick the ball to their goal.
B
101
S
39
G
134
Posts: 5,591
Reputation: 85,520

### » Tue Sep 15, 2015 10:45 pm

thanks bud, really clever. hopefully this will improve my ability to map directions
I added a goal and it works perfectly
I was using physics and a circle polygon on the ball. I'm so excited to transfer the code and see how it goes.
i never thought i'd be planning to make a football game
this puts the code on par with developers, you have my respect and gratitude. If ever i can return the favor I will
B
9
S
3
Posts: 113
Reputation: 1,009