0 Members and 2 Guests are viewing this topic.
What you want is fixed point math, which is pretty fast. Also take a look at Bresenham's algorithm. It's a very simple line drawing algorithm and I'm sure it can be used to move a bullet around.
a simpler, faster method to manage this would be to just make your playing area be larger than the actual screen resolution by some factor of two. then you can accomplish this with only a single slope calculation when the bullet is initially fired and without using anything but simple integers. what this looks like in realisation would be having a one byte each for x and y positions (or two for x, if you choose a factor greater than two and thus end up with more than 192 horizontal logical pixels mapped to your 96 physical pixels), and one byte each for the x and y velocities (or a nibble, if you want to conserve space). then just add the velocities to the positions each frame and then map the resulting logical positions to their physical positions with a quick division by your chosen factor of two (sure, other factors could be used, but they would be MUCH slower). thus, if you chose a factor of four and only set the x velocity to 1, your bullet would only "drift" over one physical pixel every 4 frames.
;Given : (x1,y1) and (x2,y2)x2-x1 → widthy2-y1 → height1 → xinc1 → yincIf width>0 1 → xinc Else -1 → xinc -width → widthIf height>0 1 → yinc Else -1 → yinc -height → heightIf width=0 y1 → count while count <= y1+height plotpixel(y1,x1) x1+1 → x1 else width → accumulator width*2 → width height*2 → height 0 → count while count < width plotpixel(y1,x1) y1+yinc → y1 accumulator - height → accumulator while accumulator < 0 x1 + xinc → x1 plotpixel(y1,x1) accumulator + width → accumulator plotpixel(y1,x1)
Lbl BNEXT.IN: Ptr.accumulator,dir,x,xinc,w, y,yinc,h,counter{r1}→r3!If {r1+1}r1+4→r1{r1++)→r2+{r1++)→r2r3-{r1++}→r3→{r1-8}r3<<0→{r1-7}Elser1+1→r1{r1++)→r2+{r1++)→r2r3+{r1++}→r3→{r1-5}r3<<0→{r1-4}r1+3→r1End{r1-5}→X{r1-2}→YIf {r1++}--ReturnEndr1-8→r1Lbl DELB.REMOVE THE BULLETReturn!If {GDB1)Exch(GDB1Z,r1,9){GDB1}--ReturnLbl ADDB.IN: (x1,y1,x2,y2)ReturnIf {GDB1}=10{GDB1}++→r6*2*2*2+r6+GDB1-9→r6r3-r1→{r6++}→r5=0→{r6++}r1→{r6++}0→{r6++}If r6>01→{r6}ElseIf r6>32767-1→{r6}-r5→r5Endr5→{r6++}r5→{r6+4}r4-r5→{r6++}→r5=0→{r6++}r2→{r6++}0→{r6++}If r6>01→{r6}ElseIf r6>32767-1→{r6}-r5→r5Endr5→{r6++}Return
-lots of helpful stuff-
.ANGLEA[00003C3C3C3C0000]->Pic1[0000001818000000]->Pic2DiagnosticOffClrDrawDrawInvStorePic.FIRING POS: 44,2530->X54->Y0->BRepeat getKey(15) RecallPic sub(D DispGraph If getKey(2) and (X!=1 X-1->X End If getKey(3) and (X!=87 X+1->X End If getKey(4) and (Y!=1 Y-1->Y End If getKey(1) and (Y!=55 Y+1->Y End .BULLET STRUCUTRE: {PTR-7}-None {PTR-6}-Accumulator {PTR-5}-Width {PTR-4}-Height .{PTR-3}-XSpeed {PTR-2}-YSPEED {PTR-1}-X {PTR}-Y !If rand^(8) .MAKE NEW BULLET B+1->B*8+L1->I 25->{I} 44->{I-1} .SET X AND Y SPEED (2 being 1, 0 being -1) If 25>Y 0->{I-2} 25-X->{I-4} Else 2->{I-2} X-25->{I-4} End If 44>X 0->{I-3} 44-X->{I-5} Else 2->{I-3} X-44->{I-5} End .SET ACCUMULATOR, W, H (I-7 is nothing) {I-5}->{I-6} {I-5}*2->{I-5} {I-4}*2->{I-4} 0->{I-7} End For(I,1,B 8*I+L1->J .CALCULATE BULLETS POSITION If {J-6}>{J-5} {J-1}+{J-3}-1->{J-1} {J-6}-{J-5}->{J-6} Else {J}+{J-2}-1->{J} {J-6}+{J-4}->{J-6} End .DELETE OFFSCREEN BULLETS If ({J}=57) or ({J}=0) or ({J-1}=94) or ({J-1}=0) 8*B+L1->K Copy(K,J,8)^^r B-1->B I-1->I End EndEndLbl D.DISPLAY BULLETS/SHIPSFor(I,1,B 8*I+L1->J Pt-Change({J-1},{J},Pic2)EndPt-Change(44,25,Pic1)Pt-Change(X,Y,Pic1)
8*I+L1->J
That's weird, sometimes there's some sort of delay of the bullets changing direction as the player moves around the screen, sometimes it happens much more quicklyCode: [Select]8*I+L1->JSo that definition defines J, which then calculates the bullets' positions. Could this be it? Also how is the code for taking the player's position on the screen taken into account?
I've been looking at the behaviour and I may have another idea why it's not completely working, though it's hard to explain and I'm not sure it happens all the time. When you move vertically up, your x value doesn't change, perhaps your program isn't always calculating the change in y value, and because the x value is not changing, the program isn't calculating a new angle for the bullets to shoot to.Just a suggestion.