• Create Account

Banner advertising on our site currently available from just \$5!

# Advanced motion prediction ( 2D ) help

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

26 replies to this topic

### #21noobnerd  Members   -  Reputation: 128

Like
1Likes
Like

Posted 14 November 2011 - 10:52 AM

Wooohoo it works!

mostly...

i managed to code it down, and just as you said, IT WORKS!
but sometimes it will give me negative values which obviously dont work. Also sometimes it gives me quite normal values which dont work???

i dont seem to be allowed to attach .exes nor .rar files?

leftclick to place the shooter, press space to "drive" the target forward, press enter to shoot and press space to return from shooting.

the value in the topleft corner is the value of t after 25 iterations of the Newton rahson method. i tried others but 25 seemed to be fine and very accurate for this quick test. ( 4 wasnt accurate enough)

oh and the litle white dot is where you should aim ( with the mouse)

the code of the newton calculation :

a# = rx#-x2# : b# = ry#-y2# : p# = pi()
v1# = 2*a#*h#*r# : v2# = 2*b#*h#*r# : v3# = -2*v#^2 : v4# = r#^2+a#^2+b#^2 : v5# = r#*2*b# : v6# = r#*2*a# : t0# = sqrt((x1#-x2#)^2+(y1#-y2#)^2)/v#
for i = 1 to 25
t0# = t0# - (v4#-t0#^2*v#^2+v5#*cos(h#*t0#+g#)+v6#*sin(h#*t0#+g#))/(v1#*cos(h#*t0#+g#)-v2#*sin(h#*t0#+g#)-v3#*t0#)
next

its DarkBasic code. might be quite unclear as it is so stacked.

rx#,ry# = coordinates of the center of the targets "circle"
x2#,y2# = coordinates of the shooter
h# = turnrate of target
v# = velocity
x1#,y1# = coordinates of target
t0# = the value of t#

any ideas of how to avoid negative and wrong values? i would rather it gave a blank than giving wrongs ...

### #22noobnerd  Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 November 2011 - 10:53 AM

well then im just gona "like" all the posts here

### #23Álvaro  Crossbones+   -  Reputation: 15395

Like
1Likes
Like

Posted 14 November 2011 - 11:01 AM

any ideas of how to avoid negative and wrong values? i would rather it gave a blank than giving wrongs ...

Yes, you should capture a specific case where the code doesn't do what you want, write down all the inputs and work step by step through the computation, verifying that your understanding of what's going on matches what the program is doing.

Feel free to post a specific example (a, b, g, h, r and bullet_speed) where the code does the wrong thing and I'll help you understand the situation.

### #24noobnerd  Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 November 2011 - 11:08 AM

while hunting down an example for you, i noticed that changin it to 250 iterations results in all invalid t# values beeing reported as -1.#IND which is what i want it to do... although it would be quite a problem if i would have to do 250 iterations just to get this ansvear

### #25noobnerd  Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 November 2011 - 11:13 AM

ok i have an ansvear for you. EVERY location that is "behind" the target will miss.

you can picture it like this :

if the radius is a line going on a graph from 0,0 to 0,2
the targets direction is negatively along the x axis
then every location inside the aproximately 135 degrees between that and a line between 0,0 and 1,-1 will miss.

and, the higher the number of iterations the smaller the amount of degrees becomes.

@ 25 its like about 110
@ 50 its almost 90
@ 100 its like 10 or somethin
and @ 250 there is nothing wrong left. only negative numbers and correct ones

### #26Álvaro  Crossbones+   -  Reputation: 15395

Like
0Likes
Like

Posted 14 November 2011 - 11:51 AM

What can I say...? You have a bug somewhere. Pick an example where the numbers are easy (say, @ 90) and follow the code step by step with a debugger. This is a really important skill that you should acquire.

### #27noobnerd  Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 November 2011 - 11:56 AM

and duh, there is another problem. it doesnt take the shooters motion into account. if i set its vector to anything else than 0 then it will always miss.

the thing is in my game the bullet will get a vector that is calculated like this:

bullet_vector = sin(shipangle)*bullet_velocity,cos(shipangle)*bullet_velocity
bullet_vector = bullet_vector + ship_vector

i will try to change the formulas to accomodate this

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS