开发者

Distance formula in java printing NaN -- How do I fix this?

开发者 https://www.devze.com 2022-12-15 03:47 出处:网络
Okay, so I\'m using the distance formula to print out the distance between two player objects from inside one of the player ob开发者_运维问答jects (With \"other\"being another Player object). How can

Okay, so I'm using the distance formula to print out the distance between two player objects from inside one of the player ob开发者_运维问答jects (With "other" being another Player object). How can I get it to just print the number without getting a "NaN"?

System.out.println("D = " + Math.sqrt(Math.pow(x - other.x, 2) - Math.pow(-(y - other.y), 2)));


You're trying to take the square root of a negative number. Add the squares, don't subtract them.

Also, negating y - other.y is unnecessary, though harmless. value2 is the same as (-value)2 for all numbers.

System.out.println("D = " + Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2)));
//                                                            ^^^
0

精彩评论

暂无评论...
验证码 换一张
取 消