calculating distance
Author |
Message |
shoobyman
|
Posted: 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
|
|
|
xHoly-Divinity
|
Posted: 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
|
Posted: 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
|
Posted: 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
THIS PROGRAM IS PISSING ME OFF!!!!!!!!!!
|
|
|
|
|
|
richcash
|
Posted: 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
|
Posted: 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 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
|
Posted: 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
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
Description: |
help me with the autopilot please |
|
Download |
Filename: |
Alien Invaders.zip |
Filesize: |
6.38 KB |
Downloaded: |
45 Time(s) |
|
|
|
|
|
|
jrblast
|
Posted: 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
|
|
|
shoobyman
|
Posted: 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
|
|
|
|
|
|
|
|