Path: chuka.playstation.co.uk!news From: "Alex Herbert" Newsgroups: scee.yaroze.programming.2d_graphics Subject: Re: What do'you mean I have to brush up on me maths! Date: Sun, 4 Jul 1999 16:15:48 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 42 Message-ID: <7lnteg$i9a1@chuka.playstation.co.uk> References: <7ko1jf$33u25@chuka.playstation.co.uk> <376fbd21.251404588@news.scea.sony.com> <7lkucs$ko217@chuka.playstation.co.uk> NNTP-Posting-Host: 195.166.144.81 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 Rad wrote in message news:7lkucs$ko217@chuka.playstation.co.uk... > Ok, could you explain this with some example code or formula. > I need to work out dx and dy when I know x1, y1, x2 and y2. > > It doesn't have to be super accurate, as long as the bullet travels roughly > in the right direction. > > Thanks, > > Rad. > > 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