Moving alone a single LINE

BlitzMax Forums/BlitzMax Programming/Moving alone a single LINE

Matt McFarland(Posted 2006) [#1]
Ok, now if you have played galaga or galaxian; sometimes the enemy decides to fly "right at you" - or more like, right where you were when the enemy sought to launch.

check this out:

caption: Path1

caption: Path2

As you can see.. if I wanted the enemy to follow the blue line in path2, how would I program that? X and Y checks dont seem to be enough. For example.. If the enemy reaches the target X before the enemy's Y is at the bottom, then the enemy goes straight down.. making the following path instead..

caption: Bad Path

As you can see, this isn't what I'm trying to do, I would like the enemy to follow Path2. But here's the kicker: The enemy's initial x and y coordinates vary when it decides to do the charge. Therefor simple x and y checks do not work. The enemy must follow a line. Now I could do this easily by making a 2 point spline, but what spline would that be?? Obviously a bezier spline would be OTT for this!


TartanTangerine (was Indiepath)(Posted 2006) [#2]
work out the vector and divide it by the distance, that will give you the movement vector.

The following code is from memory but it should work.
dx = player_x - enemy_x
dy = player_y - enemy_y
distance = Sqr(dx*dx + dy*dy)
vx = dx/distance ; Movement Vector X
vy = dy/distance ; Movement Vector Y
enemy_x = enemy_x + vx
enemy_y = enemy_y + vy



Matt McFarland(Posted 2006) [#3]
Thank you Mr.

I was given another idea..

paladin from shmup dev came up with this:
http://www.shmup-dev.com/index.php?topic=274.msg1509#msg1509

Now I was thinking.. what would be more CPU efficient??


ImaginaryHuman(Posted 2006) [#4]
You just need a linedrawing algorithm and then have the enemy be `plotted` at each point along the line. You don't need vectors unless you're using them already for all your movement and rotation stuff.


Haramanai(Posted 2006) [#5]
AngleDaniel do you have a site or two with algorithms for drawing lines and other shapes?


pappavis(Posted 2006) [#6]
It should be fairly simple to do. Indiepath provided an algorith to use (see above). The result behaviour of the code is as per description of Matt' "caption: Bad Path" :(.

What I want is: the behaviour of "caption: Path2" (see post above). No physics, curves or other neat tricks involved. The enemy shoots a bullet at the player.
The ideal algorith should be able to figure out the angle and distance to target, then travel along that calculated angle (in a straight line) until it reaches the destination.

Obviously:
The position of playerX and playerY is know.
The position of enemyX and enemyY is know.
The start of the enemy bullet equals enemyX and enemyY.
The end of the enemy bullet equals playerX and playerY.
The enemy can rotate (a cannon turret) to the direction of player.

For example (see this grid picture)
* PlayerX=4, PlayerY=11
* EnemyX=22, EnemyY=5
The enemy shoots a bullet from its XY-Y to the (last) known X-Y coordinates of the Player.
[img]http://quest.arc.nasa.gov/mars/teachers/tg/program1/1.2grid.gif[/img]

any takers, a full code example will be very much appreciated. TIA!


North(Posted 2006) [#7]
first calculate the angle to shoot at
... like

dir = ATan2 ((player.posy-evilenemy.posy),(player.posx-evilenemy.posx))

then move your bullet
...like
bullet_x:+speed*Cos(dir)
bullet_y:+speed*Sin(dir)


this would be one way to do it, maybe not the most efficient but it works


tonyg(Posted 2006) [#8]
Slight alteration.
Calculate the angle using the (pos_x/y_of_player_when_evil_enemy_decided_to_plunge)