Jump to content

[SOLVED] hard math problem.


Michdd

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/172937-solved-hard-math-problem/
Share on other sites

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?

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.

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?

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.

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);
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.