Moving alone a single LINE
BlitzMax Forums/BlitzMax Programming/Moving alone a single LINE
| ||
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! |
| ||
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 |
| ||
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?? |
| ||
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. |
| ||
AngleDaniel do you have a site or two with algorithms for drawing lines and other shapes? |
| ||
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! |
| ||
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 |
| ||
Slight alteration. Calculate the angle using the (pos_x/y_of_player_when_evil_enemy_decided_to_plunge) |