
-----------------------------------
shoobyman
Fri Nov 10, 2006 7:18 pm

calculating distance
-----------------------------------
ok, i am making a spaceship/alien invader game and i am trying to create an autopilot feature.  No matter what i do, the ship moves the wrong way (obviously a problem in my calculations).  The ship must move away form the closest bullet, but I can't seem to do that.  here is the current code.  If anyone sees the problems, please tell me.


    if autopilot = true then
        for i : 1 .. 10            %10 is the number of bullets
            distx := bulletsx (i) - shipx           %The x distance away from the %current bullet it is calculating
            if distx < closex then         %if the x distance is closer than the %closest x it has calculated so far, then the distx becomes the new closest %x
                closex := distx
            end if
        end for
        if closex > 0 then   %If the closest bulletx is >0 then move away
            shipx -= 5
        end if
        if closex < 0 then   %If the closest bulletx is  0 then
            shipx += 5
        else
            shipx -= 5
        end if
        lowest := 1000
    end if

Instead of running away from bullets now, it tries to get hit by them (and does a pretty good job of doing that).  You would think that if i just invert the code into this:

    if autopilot = true then
        for i : 1 .. 10
            bulletdistance (i) := bulletsx (i) - shipx
            if abs (bulletdistance (i)) < lowest then
                lowest := bulletdistance (i)
            end if
        end for
        if lowest > 0 then
            shipx -= 5
        else
            shipx += 5
        end if
        lowest := 1000
    end if

it would work, but now it just gets stuck in the side  :evil: 
I hate this.  I think it would help if i upload the program, so here you go.
Autopilot is automatically engaged, move around with the a,s,d,w keys.

Ignore the crappy graphics

-----------------------------------
jrblast
Sat Nov 11, 2006 12:15 pm


-----------------------------------
I think whats happening is that your calculating from the left side of the ship, but you should be calculating from the center of the ships x, (add picwidth(ship)div 2 to shipx in your calculations) also, what is happening by coincidence is that once it starts moving in one direction to avoid the bullets, it keeps moving because the nearest bullet is still in the same direction.

-----------------------------------
shoobyman
Sat Nov 11, 2006 1:13 pm


-----------------------------------
yup,  i figured it out at 1:00AM last night, but it still doesn't work that good cuz it will dodge bullets  only if the ship is 1 pixel wide lol, w/e
