Welcome, Guest. Please Login or Register.  • Help
SMF Underground
+ SHMUP-DEV » SHMUP DEV FORUMS » Assistance
|-+ Making an object move alone a single lines path...

Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read January 14, 2006, 08:55:48 PM #0
Matt McFarland

Making an object move alone a single lines path...

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!

« Last Edit: January 14, 2006, 08:59:29 PM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read January 14, 2006, 08:56:53 PM #1
paladin

Re: Making an object move alone a single lines path...

can your enemies rotate?  if so, when the enemy launches why don't you do a quick check to see what the player's coordinates are and move the enemy towards it?  move the enemy until it reaches that point.


needs more particles...
Offline  
Read January 14, 2006, 09:11:51 PM #2
Matt McFarland

Re: Making an object move alone a single lines path...

*mouth drops*

yeah they can rotate and move in their rotated direction...

I think I'll try this! Tongue Thanks! That is alot simpler than the other way I was doing it.


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read January 14, 2006, 09:33:17 PM #3
paladin

Re: Making an object move alone a single lines path...

try this...

angle = 90 + Atan2(enemyY-playerY, enemyX-playerX) : If angle < 0 angle = angle + 360


needs more particles...
Offline  
Read January 14, 2006, 09:45:13 PM #4
Matt McFarland

Re: Making an object move alone a single lines path...

Quote from: IndiePath
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
IP told me that is a way to do it over at bmax forums. I'm wondering which would be more cpu intensive.


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read January 14, 2006, 09:48:53 PM #5
Indiepath

Re: Making an object move alone a single lines path...

Well you've still got to calculate the movement vector depending on the angle when using paladins code.

You could put them both together with some limiting factor and get the enemy to swoop at your player rather than follow a direct line to them.




www.pjio.com - Upload, Tag, Share and Play.
Offline  
Read January 14, 2006, 10:04:42 PM #6
paladin

Re: Making an object move alone a single lines path...

why don't you just place the end of the spline at the x,y of the player when the enemy launches?


needs more particles...
Offline  
Read January 14, 2006, 10:16:10 PM #7
Matt McFarland

Re: Making an object move alone a single lines path...

I can do that.. but I was thinking that was a little over the top for what it's purpose is for..


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read January 14, 2006, 11:54:23 PM #8
oNyx

Re: Making an object move alone a single lines path...

>I'm wondering which would be more cpu intensive.

Doesnt matter Smiley

Because that piece of code is run only once per enemy (or not at all) and the calculation is very simple. Even if you use code there which is 10 times slower than it needs to be, it wouldnt make a difference.
Offline  
Read January 15, 2006, 05:00:28 PM #9
Sean.Doherty

Re: Making an object move alone a single lines path...

Paladin's code is similar to what I presently use.  As for CPU, neither is really going to make any measurable difference in a vertical shooter.  Also, paladin's code calculates the angle which you will need if you want to rotate the enemies image? 


Sean Doherty, BCS
www.GameSlammer.com
Offline  
Read January 15, 2006, 05:17:58 PM #10
Matt McFarland

Re: Making an object move alone a single lines path...

Well, I already have an angle function so all I had to do was this:
Code:
Method DoChargeAttack(obj:TEnemy)
Local enemy:TEnemy = obj:TEnemy
enemy.Angle = calcAngle2D( enemy.x,enemy.y,  enemy.Target_x,800)
If enemy.y < 700
enemy.x:+ Cos(enemy.Angle)* Delta.time() * 3
enemy.y:+ Sin(enemy.Angle)* Delta.time() * 3
End If
If enemy.y > 700 ' Put enemy at top and have it move back into formation.
enemy.y=-100
Enemy.ReturnX = Enemy.x ; Enemy.ReturnY = Enemy.y ; enemy.t = 0 ; enemy.SingleAttackPhase = 4
enemy.phase = singleattack
End If
End Method

This is how I calculate my 2D angles:
Code:
Function calcAngle2D:Float( x1:Float, y1:Float, x2:Float, y2:Float )
Local Angle:Float=ATan2( ( y2 - y1 ), ( x2 - x1 ) ) '+ 180
Return Angle
End Function
I got this code from the bmax forums a while ago, cant remember who shared it though..
« Last Edit: January 15, 2006, 05:22:21 PM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Pages: [1]   Go Up
Jump to:  

Page created in 0.078 seconds with 17 queries.