Welcome, Guest. Please Login or Register.  • Help
SMF Underground
+ SHMUP-DEV » SHMUP DEV FORUMS » Assistance
|-+ Expanding and Contracting like in Galaga

Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read December 23, 2005, 02:57:23 PM #0
Matt McFarland

Expanding and Contracting like in Galaga

You know how in Galaga the enemies sort of expand and contract from each other? I'm really having a hard time thinking of a logical way to do this.

My first attempt yesterday was to make additional points for them to go to. So when I assign them their coordinates, they are also assigned additional points (the expanded point, and contracted point)

That was easy, but then after I tested it I found out that they all dont move in uniform and they move too choppy. So its back to the drawing board for me.

Anyways, I've asked around and was given some information about an epicentre, and use cosine(time) * scale factor * original distance from epicenttre.  and use slightly different scale factors for X and Y for more elliptical expansion.  Could anyone tell me how they would code this?  Perhaps its something simple to understand?
« Last Edit: December 29, 2005, 02:00:06 AM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 23, 2005, 03:19:20 PM #1
paladin

Re: Expanding Contracing like in Galaga

expand and contract... you mean during idling?  the pulsating effect?


needs more particles...
Offline  
Read December 23, 2005, 03:20:15 PM #2
Matt McFarland

Re: Expanding Contracing like in Galaga

Yeah, right when they're idling.


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 23, 2005, 03:29:02 PM #3
paladin

Re: Expanding Contracing like in Galaga

ah, i thought that was part of the sprite animation.  i haven't worked with 2D enough to know the answer if it's otherwise.


needs more particles...
Offline  
Read December 23, 2005, 03:47:26 PM #4
Taiphoz

Re: Expanding Contracing like in Galaga

there are a few ways to do this, none of which involve animated sprites.

Give each row a Width, lets say a row has a width of 200 pixels, and each row has 5 or so bad guys in it.
To position your enemies, simple take the 200 and / it by the number of aliens in the row, add to that the screen offset to make sure they are in the middle of the screen and then place your sprites there..

wave_width=200
aliens_per_row=5
wave_step = wave_width / alines_per_row

wave_position_on_screen = 400 (near the middle)

for loop = 1 to aliens_perrow
  drawsprite wave_position_on_screen + loop * wave_step , y , frame
next

Just update your wave width and recalculate your wave step per tick. and you will have your pulsing effect.

Hope this helps.


Offline  
Read December 23, 2005, 04:20:11 PM #5
Matt McFarland

Re: Expanding Contracing like in Galaga

Not sure I follow it that well.  I see that you have a wave width?  But I'm not sure they're going to have a wave affect.   I already have them positioned.  After they reach their positions, they are currently following a script that has them move left and right, but there is no expand / contract effect.  My previous attempt (as per my first post on this thread) made them look worse! So now I have to figure out hwo to do it again.

Are you saying that I add a "wave" effect that alters their x and y coordinates?  Now I find it interseting that you are taking into accoutn all the pixels that all the enemeis in one row take up.  But I think that it just to keep them in the center? 

Well, what I'm trying to do is have them expand and contract like in Galaga.  Its very subtle and they dont move that much, but it does look very nice. 
« Last Edit: December 23, 2005, 04:24:47 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 December 23, 2005, 04:43:36 PM #6
Taiphoz

Re: Expanding Contracing like in Galaga

take the word wave and replace it with (alien level)

By wave I simply mean this batch of alines.

And what I'm describing will give you the Expand and contract . I will try to explain it again in a simpler manner.

You take a line of aliens. say its got 5 per line, if you measure from the left alien to the far right alien its 100 pixel's this is your alien line width, ie how much space that line of alines is taking up on the screen. NOW. lets say you want to make sure that your 5 aliens are spread out evenly over that 100 wide pixel area, you would position them at ever 100/5 'th pixel.. so 1...20....40...60....80....100 like that.

NOW. if your line width is a variable called wave_width or what ever it was I called it. you could make your width 120 pixels instead of 100, and again get each of their new positions by doing 120/5 where 5 is the number of aliens per line.

so looking at my code above, all you have to do is update the width of the line with a sin wave, and that will create your pulsing effect.

I think thats about as simple as I can make it without actually coding it for you, which I dont have time to do atm.


Offline  
Read December 23, 2005, 04:54:04 PM #7
Matt McFarland

Re: Expanding Contracing like in Galaga

Ahh!! Very very clever! Thanks a bunch! (I get it now)  Now what if I wanted them to move up and down a little bit too?


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 23, 2005, 05:56:15 PM #8
Taiphoz

Re: Expanding Contracing like in Galaga

getting them to just move up and down as they move along the screen should be a walk in the park now.


Offline  
Read December 24, 2005, 04:24:18 PM #9
Matt McFarland

Re: Expanding Contracing like in Galaga

I'm going to have to make a separate "prototype" to test this out, as I see it's a theory for now.  Unfortunately, I've been doing the "Holiday Dance" for a bit and I haven't been able to do much, also battling sleep deprivation.. haha..


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 25, 2005, 12:44:52 AM #10
Matt McFarland

Re: Expanding Contracing like in Galaga

so looking at my code above, all you have to do is update the width of the line with a sin wave, and that will create your pulsing effect.

Hmm, could you put a snippet on doing a sin wave?


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 25, 2005, 02:29:06 AM #11
Matt McFarland

Re: Expanding and Contracting like in Galaga

OK..

The width of 12 aliens is 384 pixels..

They are all 32 wide, 32 long.

How do I do this!?!?


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 25, 2005, 02:53:13 AM #12
Matt McFarland

Re: Expanding and Contracting like in Galaga

I got it to work!!

But one problem.. The stretch goes to the right (it's not centered)

IT looks like this..

STEP 1:

x x x x x

STEP 2:
x x x x  x

STEP 3:
x x x  x   x

STEP 4:
x x  x   x    x

« Last Edit: December 25, 2005, 03:45:34 AM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 26, 2005, 09:20:06 AM #13
Matt McFarland

Re: Expanding and Contracting like in Galaga

A guy they call "Dev" over at the blitzbasic forums gave me extensive help (over IRC, then ICQ) and I have officially finished this daunting task.  I owe him big thanks Wink 

Pretty much I'm using the  sin() method and it looks great!!!  I'm glad I finally got past this obstacle, so now I can work on other stuff involving the game Smiley

The greatest thing about all of this is that I have finally gained the comprehension of how to use the sin function!! woot! Smiley
« Last Edit: December 26, 2005, 09:21:54 AM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 28, 2005, 07:19:26 AM #14
Indiepath

Re: Expanding and Contracting like in Galaga

Try and precalculate your sin and cos, it'll save you loads of CPU.




www.pjio.com - Upload, Tag, Share and Play.
Offline  
Read December 28, 2005, 11:15:02 PM #15
Indiepath

Re: Expanding and Contracting like in Galaga

Now "flocking" would be incredibly organic and very different.




www.pjio.com - Upload, Tag, Share and Play.
Offline  
Read December 28, 2005, 11:27:07 PM #16
paladin

Re: Expanding and Contracting like in Galaga

wasn't there was flocking in Pleiades?  man... i'm going WAY back.


needs more particles...
Offline  
Read December 30, 2005, 11:54:42 PM #17
Taiphoz

Re: Expanding and Contracting like in Galaga

yeah create a sin cos lookup table with an array.

doing anything with sin or cos in a main loop will draw a ton of cpu time, if your pre calc it and store the data into a lookup table then you will save a lot of CPU time.

I would have suggested using sin and cos but didnt think it was needed plsu I at the time haddnt thought about a lookup table so i thot it would be to heavy on the cpu cycles..

anyway let us see some code or explain hwo your system works.


Offline  
Read December 30, 2005, 11:57:12 PM #18
Matt McFarland

Re: Expanding and Contracting like in Galaga

Code:
Local FormCenter# = Sin(time)
Local StretchScale# = (1.0-Abs(FormCenter)) * Enemy.CenterID
Enemy.Assignedx = LevelCenter + (FormCenter * WaveWidth) + (Enemy.CenterID * 40) +  (StretchScale * StretchWidth)
Enemy.AssignedY = enemy.SpawnAssignedY + Sin(time*4) * (WaveY_Width) + Sin(-time*3) * (enemy.RowID * 10)

Hmm as you can see, a lot of the constants I used are dependant on what level you're on.  That way the enemies move differently depending what level it is.  Kinda a neat concept.  Do you think look up tables are really necessary? 


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 30, 2005, 11:59:50 PM #19
Taiphoz

Re: Expanding and Contracting like in Galaga

yup thats prity much exactly what i was trying to explain to you, minus the sin calls tho. you DEF have to get rid of those.

for x=1 to 360
  sinL(x)=sin(x)
next

wave= sinL(value)

god been ages since i even though about using a lookup table but thats about the jist of it.


Offline  
Read December 31, 2005, 12:02:08 AM #20
Matt McFarland

Re: Expanding and Contracting like in Galaga

yup thats prity much exactly what i was trying to explain to you, minus the sin calls tho. you DEF have to get rid of those.

for x=1 to 360
  sinL(x)=sin(x)
next

wave= sinL(value)

god been ages since i even though about using a lookup table but thats about the jist of it.

Hmm all I have to do is replace the sin args with sinL args?  (after making the sinL array) ?


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read December 31, 2005, 12:56:05 AM #21
Olofson

Re: Expanding and Contracting like in Galaga

Try and precalculate your sin and cos, it'll save you loads of CPU.
Have you actually benchmarked this? What hardware are we talking about?

Indeed, the FPU trig functions (which are hardware LUT based, AFAIK) are (still) slower than less accurate approximations - but that's something I worry about when doing real time audio DSP stuff, not when moving a few tens of objects around at 60-100 fps...

Then again, we're not talking about a major coding effort here. Keep in mind though, that a lot of things change across CPU generations, and on reasonably modern hardware (P-II and later, or even Pentium), LUTs often turn out to be slower, mostly due to cache misses. Memory bandwidth and latency can't keep up with the evolution in raw CPU core speed.

Either way, the most important rule is to avoid premature optimization! Make it work first, then make it work fast. Profile before and after, preferably on various hardware, to make sure that you're actually improving things.

(You probably knew all that already, but anyway... Wink )


//David
Offline  
Read December 31, 2005, 02:00:28 PM #22
Taiphoz

Re: Expanding and Contracting like in Galaga

yup thats prity much exactly what i was trying to explain to you, minus the sin calls tho. you DEF have to get rid of those.

for x=1 to 360
  sinL(x)=sin(x)
next

wave= sinL(value)

god been ages since i even though about using a lookup table but thats about the jist of it.

Hmm all I have to do is replace the sin args with sinL args?  (after making the sinL array) ?

Yeah prity much, make sure and test it first tho, in essence all your doing is calculating the sin value from 1 to 360 and dumping them into an array, when you call the array though you will probably have to abs() the value to make sure its not a float or real number.


Offline  
Read January 02, 2006, 02:39:30 AM #23
solidcube

Re: Expanding and Contracting like in Galaga

This is similar to scaling...

Code:
expandfactor[for this tick] = sin(time);
if (alienx > screenmiddle) alienx += expandfactor; else alienx -= expandfactor;
Or something to that effect...
Offline  
Read January 03, 2006, 05:41:24 PM #24
Bursar

Re: Expanding and Contracting like in Galaga

Matt,

Any chance you can post your code for this, as I gather you have it done now Smiley I'd be interested to see how it all works.
Offline  
Read January 03, 2006, 07:51:57 PM #25
Matt McFarland

Re: Expanding and Contracting like in Galaga

BLitzMax

Ok First, I set the following constants up:
Code:
' My playfield is 600 pixels wide, this is the center.
LevelCenter = 300
 ' wavewidth Tells the code how far left and right the whole group moves left and right, in my case, not far at all.
wavewidth:Float = 4       
 ' WaveY_Width Tells the code how far they Stretch out downward
WaveY_Width:Float = 2 
' StretchWidth tells the code how far they stretch out from center
StretchWidth:Float = 15   

Second, I set the following variables up when an alien is created
Enemy.CenterID
   0 is the middle guy, -1 is to his left, 1 is to his right and so on (2 left over is -2, 2 over right is 2, etc)
   This tells the code at the bottom to stretch them out from the center.
Enemy.RowID
   1 is the top row, and so on.. This tells the code at the bottom which row moves down more (stretching effect)

Third, we set up our sin timing. 
basically the Time-Variable is nothing but a float variable that starts at zero and goes up by any number per cycle.  I have timestep go up by .3 every cycle.  The higher the number the faster they go.
Here's what I use:
Code:
timestep:float = .3
time:Float = time:Float + timestep:Float

Ok Fouth the following script is fired on every enemy object every cylce...
Code:
Local FormCenter:Float = Sin(time)
Local StretchScale:Float = (1.0-Abs(FormCenter)) * Enemy.CenterID
Enemy.Assignedx = LevelCenter + (FormCenter * WaveWidth:Float) + (Enemy.CenterID * 40) +  (StretchScale:Float * StretchWidth:Float)
Enemy.AssignedY = enemy.SpawnAssignedY + Sin(time*4) * (WaveY_Width:Float) + Sin(-time*3) * (enemy.RowID * 10)

Any Questions? I will gladly answer as best as possible...
« Last Edit: January 04, 2006, 03:07:55 AM 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 03, 2006, 09:38:01 PM #26
Bursar

Re: Expanding and Contracting like in Galaga

OK, I think I can follow most of that. Not being a Blitz person, what is 'time' when you're calling sin(time)?

And I take it that if you have a line of 4 enemies, you would set:
Enemy.CenterID  -2
Enemy.CenterID  -1
Enemy.CenterID  1
Enemy.CenterID  2

As you move from left to right along the row.
« Last Edit: January 03, 2006, 09:43:43 PM by Bursar »
Offline  
Read January 03, 2006, 10:30:53 PM #27
Matt McFarland

Re: Expanding and Contracting like in Galaga

modified it, that should help ya! Sorry forgot to put the time part!!! which is a very important part too...


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read January 04, 2006, 09:35:53 AM #28
Bursar

Re: Expanding and Contracting like in Galaga

Excellent, that makes a bit more sense Smiley

I'm just running it once at the moment so I can see the effect of the spread. I think I need to play with the numbers a bit as they aren't positioning themselves as I expect them to.

Then it's a case of getting them to glide gracefully from their start positions to their expanded postions and back again.

Here's a before and after shot, I've already reduced the numbers in the after shot, but more tweaking is definately required!

* The graphics are just handy place holders at the moment Smiley *

[attachment lost, please re-upload]
Offline  
Read January 04, 2006, 01:54:21 PM #29
Bursar

Re: Expanding and Contracting like in Galaga

OK, got it all sorted now, and have managed to tweak the speeds that the invaders expand and contract at. Initially they were all moving at the same speed, but covering different distances. Although it looked quite cool as they fanned out, the invaders got out of sync.

A quick bit of maths based on how far the invader has to travel and they all now arrive at their expanded points at the same time. I might stick the fanning bit back in as the first expansion move, and then after that have them all move at the same speed.

Thanks for the code, it was most useful Smiley I'll post what I did here, just incase anyone else is using Torque2D and wants to achieve a similar effect.

Global variables:
Code:
// Base numbers used to derive the speed the invaders need to move at
$invaderBaseSpeed = 20;
$invaderBaseDistance = 20;

// Are the invaders expanding (true) or contracting (false)?
$expanding = true;

// As per Matts instructions, but with the numbers tweaked. The middle of my screen is actually 0,0
$levelCentre = 0;
$waveXWidth = 2;
$waveYWidth = 2;
$stretchWidth = 6;
$time = 0;
$timeStep = 0.1;

Each invader has the following attributes added to it as it's created per Matts instructions:
midID
colID
rowID
startPos - This one keeps track of where the invader lives when contracted


Then we generate the expanded coords using a slightly modified version of Matts code. Right at the end, we add the expanded coords to the invader. This means we only need to calc them once and then we can just read them from the invader:
Code:
function expandInvaders()
{
$time += $timeStep;
%formCentre = mAbs(mRadtoDeg(msin($time)));

%count = InvaderWaveSet.getCount();
for (%i=0; %i<%count; %i++)
{
%obj = InvaderWaveSet.getObject(%i);

%stretchScale = (1 - %formCentre) * %obj.midID;
%xval = $levelCentre + (%formCentre * $waveXWidth) + (%obj.midID * 20) + (-%stretchScale * $stretchWidth);
%yval = GetWord(%obj.startPos,1) + %formCentre * ($waveYWidth) + (%formCentre*1.5) * (%obj.rowID * 2);
%obj.expandPos = (%xval SPC %yval);
}
}

Now we need to know how far apart the start position and expanded position for the invader is, so here's a handy function to do that:
Code:
function distBetween(%loc1, %loc2)
{       
        %x1 = getWord(%loc1, 0);
        %y1 = getWord(%loc1, 1);

        %x2 = getWord(%loc2, 0);
        %y2 = getWord(%loc2, 1);

        %xd = %x2 - %x1;
        %yd = %y2 - %y1;
       
        return mSqrt((mPow(%xd,2)) + (mPow(%yd,2)));
}

Finally, we move the invaders about:
Code:
function moveInvaders(%expanding)
{
%count = InvaderWaveSet.getCount();
for (%i=0; %i<%count; %i++)
{
%obj = InvaderWaveSet.getObject(%i);

%distance = distBetween (%obj.startPos, %obj.expandPos);
%invaderVelocity = $invaderBaseSpeed * (%distance / $invaderBaseDistance);

if ($expanding)
{
* Actual command edited out for EULA reasons *
} else
{
* Actual command edited out for EULA reasons *
}
}
$expanding = !$expanding;
}

So there, piece of cake Smiley
Offline  
Read January 05, 2006, 05:04:42 PM #30
Bursar

Re: Expanding and Contracting like in Galaga

I know I'm clogging up this thread a bit, but something went wonky with my coding, and after leaving the invaders pulsing for a couple of minutes they got all out of sync and ended up in odd positions. This is despite the fact that the sin stuff is only calculated once, and after that, they just move between the starting point and the expanded point.

I can only guess it's some kind of error in the underlying engine, as something between A and B should continue moving between those exact same points. There are no calculations going on that might cause rounding errors and hence cause the invaders to drift out of place.

So I've binned all of that code, and have started doing it a completely different way. If I get it right, I should have a lot more control over the way the invader fleet moves. I don't think there's anything on TV tonight, so fingers crossed for good progress Smiley
Offline  
Pages: [1]   Go Up
Jump to:  

Page created in 0.151 seconds with 17 queries.