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, 06 Jul 1999 16:53:52 GMT Organization: SCEA News Server Lines: 46 Message-ID: <37823145.682258342@news.scea.sony.com> References: <7ko1jf$33u25@chuka.playstation.co.uk> <376fbd21.251404588@news.scea.sony.com> <7lkucs$ko217@chuka.playstation.co.uk> <7lnteg$i9a1@chuka.playstation.co.uk> NNTP-Posting-Host: vmlabs135.vmlabs.com X-Newsreader: Forte Free Agent 1.11/32.235 >Hi Rad, > >First find the distance between the two objects: > > dx = x1 - x2 > dy = y1 - y2 > >Next find the length of the line between the two objects: > > dxy = square_root( dx*dx + dy*dy ) > >And now the vector: > > vx = (dx * speed) / dxy > vy = (dy * speed) / dxy > > >I think that'll do the trick. Due to the integer maths, there will be >accuracy problems if the speed is low, or if the two objects are very close, >but this can be fixed by using fixed point if necessary. > >Herbs You can avoid the squareroot by doing an inverse tangent instead. Whether this is faster or not I'm not sure. First, find dx & dy like above. Then find the angle of this line: Angle = inverse_tan(dy / dx) Once you have the angle you can get the vx & vy components using sin and cos: vx = cos(angle) vy = sin(angle) I hope I didn't get those switched. Use a look-up table for the tan, sin, and cos to speed things up. The inverse tangent takes a bit longer despite using a look-up table. This may or may not actually be faster than doing the sqrt() as Alex suggests. That would be an interesting experiment (and one I probably should have performed before blindly going with the inverse tan method). Scott