Michdd Posted September 3, 2009 Share Posted September 3, 2009 Sorry for the bad topic title, I couldn't think of anything to describe it.. Basically I have something like this inside of a function: $speed = 5; $dx = $finalx - $currentx; $dy = $finaly - $currenty; $angle = atan2($dy, $dx); $vx = cos($angle) * $speed; $vy = sin($angle) * $speed; if($currentx + $vx < $finalx && $currenty + $vy < $finaly) { $currentx += $vx; $currenty += $vy; } else { $currentx = $finalx; $currenty = $finaly; } The function is called multiple times (currentx/y and finalx/y would be global variables). It slowly increases $currentx/y to $finalx/y. However this only works when currentx and currenty are both smaller than finalx and finaly. Can someone help me make it so this will work for all 4 scenarios? Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/ Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 if($currentx + $vx < $finalx && $currenty + $vy < $finaly) { $currentx += $vx; $currenty += $vy; } else { $currentx = $finalx; $currenty = $finaly; } Should be if($currentx + $vx < $finalx) { $currentx += $vx; } else { $currentx = $finalx; } if($currenty + $vy < $finaly) { $currenty += $vy; } else { $currenty = $finaly; } However, they'll still both stop when they reach $finalx/y, but they just don't have to BOTH be lower for one to work... you know? Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911452 Share on other sites More sharing options...
Michdd Posted September 3, 2009 Author Share Posted September 3, 2009 Sadly, that doesn't solve my problem. I'll try to explain exactly what I need a bit more. I need $currentx/y to incrementally go up or down till it reaches $finalx/y. This has to do with something on a coordinate plane, so there are 4 scenarios. finalx/y will always be currentx + 20. here's an example.. $wa = true; $finalx = $currentx + 20; $finaly = $currenty + 20; //OR $finalx = $currentx - 20; $finaly = $currenty - 20; //OR $finalx = $currentx - 20; $finaly = $currenty + 20; //OR $finalx = $currentx + 20; $finaly = $currenty - 20; // Do what roughly what I originally posted Then in the original part of code that I posted the else is triggered and $currentx/y is set to $finalx/y then $wa should be set to false, which will stop the loop. Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911455 Share on other sites More sharing options...
Daniel0 Posted September 3, 2009 Share Posted September 3, 2009 What are you actually trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911461 Share on other sites More sharing options...
Garethp Posted September 3, 2009 Share Posted September 3, 2009 if($currentx < $finalx) { $currentx += $vx; } else if($current > $finalx) { $currentx += $vx; } else { $currentx = $finalx; } if($currenty < $finaly) { $currenty += $vy; } else if($currenty > $finaly) { $currenty -= $vy; } else { $currenty = $finaly; } How about that? Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911464 Share on other sites More sharing options...
Michdd Posted September 3, 2009 Author Share Posted September 3, 2009 On an isometric scale (which is why both x and y are either increased or decreased by 20) I'm trying to make it so that it slowly (dependant on the $speed) increments the points from $currentx/y to $finalx/y. The first example I showed works when the final values are larger than the current values. However it doesn't work in any of the other scenrios that I explained, it just goes directly to the final values without any slow incrementation; although that's clear why:(Because the else statement triggers), I'm unsure how to do the math so it'll work in all cases. Edit: Garethp, that didn't work either. Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911465 Share on other sites More sharing options...
sasa Posted September 3, 2009 Share Posted September 3, 2009 try <?php function my_move($start, $end, $speed){ if (($start[0] - $end[0]) * ($start[0] - $end[0]) + ($start[1] - $end[1]) * ($start[1] - $end[1]) <= $speed * $speed) return $end; $angle = atan2($end[1] - $start[1], $end[0] - $start[0]); return array($start[0] + cos($angle) * $speed, $start[1] + sin($angle) * $speed); } $currentXY = array(100, -100); $finalXY = array(-200, 300); $speed = 1; while ($currentXY != $finalXY and $i++<1000){ print_r($currentXY); $currentXY = my_move($currentXY, $finalXY, $speed); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911649 Share on other sites More sharing options...
Michdd Posted September 3, 2009 Author Share Posted September 3, 2009 try <?php function my_move($start, $end, $speed){ if (($start[0] - $end[0]) * ($start[0] - $end[0]) + ($start[1] - $end[1]) * ($start[1] - $end[1]) <= $speed * $speed) return $end; $angle = atan2($end[1] - $start[1], $end[0] - $start[0]); return array($start[0] + cos($angle) * $speed, $start[1] + sin($angle) * $speed); } $currentXY = array(100, -100); $finalXY = array(-200, 300); $speed = 1; while ($currentXY != $finalXY and $i++<1000){ print_r($currentXY); $currentXY = my_move($currentXY, $finalXY, $speed); } ?> There's a small problem with that, but it almost works. The problem is that there should be an if statement in there so that if the starting numbers are within a small range (range depends on the speed) they're just set to the ending numbers. Otherwise sometimes the final result is off by a little. Never mind, it was my fault, because the print_r was before the function in the loop it didn't show the final result. Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911748 Share on other sites More sharing options...
Michdd Posted September 3, 2009 Author Share Posted September 3, 2009 After testing it in what I needed it works perfect. Thanks a lot!! Quote Link to comment https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/#findComment-911776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.