Author Topic: Polar to rect not working rite  (Read 715 times)

0 Members and 1 Guest are viewing this topic.

Offline chutem

  • Veteran
  • *****
  • Posts: 1119
Polar to rect not working rite
« on: July 25, 2008, 12:26:01 am »
I wanna make some bullets fire out of a dude at specific angles (refer to diagram) in a series that makes it like a double radar thingie.

This is the code I wrote:
Code: [Select]
if SPAWN_BULLETS = True then begin
  if BULLET_COUNTER = 4 then
    BULLET_COUNTER:= 1;
  else
    BULLET_COUNTER:= BULLET_COUNTER + 1;
  CreateBullet(GetPlayerStat(BOT_CHALLENGER,'x'), GetPlayerStat(BOT_CHALLENGER,'y'),20*sin((BULLET_COUNTER*45)+5),20*cos((BULLET_COUNTER*45)+5),0, 1,BOT_CHALLENGER);
  CreateBullet(GetPlayerStat(BOT_CHALLENGER,'x'), GetPlayerStat(BOT_CHALLENGER,'y'),20*sin((BULLET_COUNTER*45)+395),20*cos((BULLET_COUNTER*45)+395),0, 1,BOT_CHALLENGER);
  CreateBullet(GetPlayerStat(BOT_CHALLENGER,'x'), GetPlayerStat(BOT_CHALLENGER,'y'),20*sin((BULLET_COUNTER*45)+175),20*cos((BULLET_COUNTER*45)+175),0, 1,BOT_CHALLENGER);
  CreateBullet(GetPlayerStat(BOT_CHALLENGER,'x'), GetPlayerStat(BOT_CHALLENGER,'y'),20*sin((BULLET_COUNTER*45)+185),20*cos((BULLET_COUNTER*6.28*45)+185),0, 1,BOT_CHALLENGER);
end;
The only thing I need help with is the conversion from polar(velocity,angle) to rectangular(X velocity,Y velocity)

The bullets come out really effed up, so some help would be appreciated.
(If you aren't sure what all the variables are, just ask and I will explain them (I think they are fairly obvious))

EDIT: I should probably said that it isn't coming out right, like 3 bullets on the left and one up lol.
« Last Edit: July 25, 2008, 01:17:47 am by chutem »
1NK3FbdNtH6jNH4dc1fzuvd4ruVdMQABvs

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Polar to rect not working rite
« Reply #1 on: July 27, 2008, 04:13:27 am »
1) you oughta express the angles in radians, or use a function to convert your degrees into radians
2) try adapting these functions from MMod (all copyrights are pwnt by Avarax i think):
Code: [Select]
function deg2rad(const deg: single): single;
begin
  result:=deg/(180/pi);
end;

function Aim(const X1,Y1,X2,Y2: single) : single;
begin
  if (X2 - X1)<>0 then begin
    if X1 > X2 then
      result:= arctan((y2 - y1) / (x2 - x1)) + Pi
    else
      result:= arctan((y2 - y1) / (x2 - x1));
  end
  else begin
    if Y2 > Y1 then result:= Pi/2 + Pi/4;
    if Y2 < Y1 then result:= -Pi/2 + Pi/4;
  end;
end;

procedure Shoot(const X1,Y1,X2,Y2,speed,power,decentralize,accuracy: single; const owner,style: byte);
var AccuracyMarginal: integer;
    angle: single;
begin
  If Accuracy < 100 then
    AccuracyMarginal:=round((100 - Accuracy) / 100 * 3600)
  else
    AccuracyMarginal:=0;
  angle:=Aim(X1,Y1,X2,Y2);
  CreateBullet(X1+decentralize*cos(angle),Y1+decentralize*sin(angle),cos(angle + deg2rad(random(-AccuracyMarginal,AccuracyMarginal) / 10))*speed,sin(angle + deg2rad(random(-AccuracyMarginal,AccuracyMarginal) / 10))*speed,power,style,owner);
end;