Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 calculating distance
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Fri Nov 10, 2006 7:18 pm   Post subject: 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.

code:

    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 move away
            shipx += 5
        end if
    end if


help would really be appreciated
Sponsor
Sponsor
Sponsor
sponsor
xHoly-Divinity




PostPosted: Fri Nov 10, 2006 7:48 pm   Post subject: (No subject)

well, first of all, you should probably use pythagoran theorem for calculating distance. Since here you are doing it for 'x' coordinates only. Doesn't really matter though.

Second, you have to use an array to store distance values for every bullet. Soo... something like:

code:

var bulletdistance : array 1 .. 100 of int
var highest : int
for i : 1 .. 100
    bulletdistance (i) := bulletsx (i) - shipx
    if bulletdistance (i) > highest then
        highest := bulletdistance (i)
    end if
end for
xHoly-Divinity




PostPosted: Fri Nov 10, 2006 7:50 pm   Post subject: (No subject)

Sorry, the code above checks for greatest distance. If you want lowest distance, then you would check for 'less than' rather than greater than. It would make sense then if you named the variable lowestDistance as opposed to highestDistance since that is what ur lookin for :p
shoobyman




PostPosted: Fri Nov 10, 2006 8:06 pm   Post subject: (No subject)

i have already tried your way, but the problem is your way only works for calculating the closest positive distance to a bullet. It will not work for negative bullets Crying or Very sad
THIS PROGRAM IS PISSING ME OFF!!!!!!!!!!
Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad Evil or Very Mad
richcash




PostPosted: Fri Nov 10, 2006 8:28 pm   Post subject: (No subject)

The abs () function returns the absolute value of the argument passed to it. In other words, if the parameter value is positive, it will stay the same; if it is negative, the function will return the value without the negative sign.

abs (3) is 3
abs (-4) is 4
Clayton




PostPosted: Fri Nov 10, 2006 9:51 pm   Post subject: (No subject)

Why not just use the handy function Math.Distance(x1,y1,x2,y2 : int) ? This way you don't have to deal with negative numbers Very Happy If you don't have it, make it yourself! Here's the distance formula if you dont know it:
code:


dist = sqrt((x2-x1)^2 + (y2-y1)^2)



Take if from there!
shoobyman




PostPosted: Fri Nov 10, 2006 10:16 pm   Post subject: (No subject)

eh, i want to come up with my own formulas, it helps me learn.
I changed the code into this
code:

    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

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:
code:

    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 or Very Mad
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



Alien Invaders.zip
 Description:
help me with the autopilot please

Download
 Filename:  Alien Invaders.zip
 Filesize:  6.38 KB
 Downloaded:  45 Time(s)

jrblast




PostPosted: Sat Nov 11, 2006 12:15 pm   Post subject: (No subject)

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.
Sponsor
Sponsor
Sponsor
sponsor
shoobyman




PostPosted: Sat Nov 11, 2006 1:13 pm   Post subject: (No subject)

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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: