Path: chuka.playstation.co.uk!scea!peter_alau@playstation.sony.com From: shade@dragonshadow.com (Scott Cartier) Newsgroups: scee.yaroze.programming.2d_graphics Subject: Re: What do'you mean I have to brush up on me maths! Date: Tue, 22 Jun 1999 17:07:07 GMT Organization: SCEA News Server Lines: 55 Message-ID: <376fbd21.251404588@news.scea.sony.com> References: <7ko1jf$33u25@chuka.playstation.co.uk> NNTP-Posting-Host: vmlabs98.vmlabs.com X-Newsreader: Forte Free Agent 1.11/32.235 >At the moment I have an enemy ship which only fires forward but I want the >bullet to travel in the direction of the player, but not follow the player. >So the bullet uses the player's x and y position when it is first fired and >continues traveling in that direction until it leaves the screen. > >So any ideas on how I can do this? You know the enemy location and the player location so you can use those two pairs of X,Y coordinates to form a vector. Convert the vector to a unit vector so the distance doesn't affect the speed of the bullet. Then each frame you add this vector to the current bullet position to make it move. Multiply each component of the unit vector by some value to make the bullet go faster/slower. Take note. Unless your bullet is moving very quickly, you'll need to make sure you have enough precision in your X,Y to avoid cumulative rounding errors. For instance, if your bullet vector turns out to be dX=4.1 dY=3.9 (43.6 degrees) then when you add this to your sprite location it will only add the decimal part dX=4 dY=3 (36.9 degrees). A noticable difference. >If I increment a variable every frame/program loop (i.e. counter++) >and want to do something if counter reaches x >then is it safe to do a test like this: if (counter == x) >or is it better to do this: if (counter >= x) ? First thing, unless you reset the counter once it reaches 'x' then you need to use the first 'if'. Using the second one will cause the 'if' to be evaulated as TRUE every frame after it reaches 'x' (which may or may not be your desired affect). In general you're safe using the 'if (counter == x)'. >I take it the second 'if' performs two 'if' statements where as the first >only performs one which makes the first one quicker? Not necessarily. I don't know the instruction set of the processor in the Yaroze, but most processors can test all conditions in the same amount of time (==, !=, <, >, >=, <=). > But is it safer? (i.e. to assume that counter will ALWAYS increment by one). As long as you can guarantee the code that increments the counter will be called once and only once then yes it's safe. If you have two pieces of code that can potentially increment the counter, or if you could call the incrementing function more than once, then it might not be safe. Depends on how confident a coder you are :) Scott