Recent

Author Topic: (Slved)Facing a mathematical problem..  (Read 2291 times)

codeman

  • Jr. Member
  • **
  • Posts: 78
(Slved)Facing a mathematical problem..
« on: January 26, 2015, 11:45:25 pm »
Hello, as im saying in my subject i have a mathematical problem, i cant figure out how to make a
Tshape.circle to follow the line like this (http://postimg.org/image/klnvqbcw5/)
I click anywhere inside the client area and i want that circle to follow the 1st line only until it reaches the target (the point i clicked). In other words i need the shortest path to the target... 
Can u help?  Thanks in advance.
« Last Edit: January 27, 2015, 11:26:10 pm by codeman »
Lazarus v2.02 Win10

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Facing a mathematical problem..
« Reply #1 on: January 27, 2015, 12:07:13 am »
In vector math, "unit distance" to any angular direction is 1. If you get Point2-Point1 and divide it by the distance, you have delta-vector (how much and where the sphere moves per tick).

Single 2D vector has X and Y, so the delta calculation is
Code: [Select]
var dx, dy, dist: single;
...
  dist:=hypot(p2.x-p1.x, p2.y-p1.y);
  dx:=(p2.x-p1.x)/dist;
  dy:=(p2.y-p1.y)/dist;
...
Then you can simply add dx and dy to sphere position, until dist is under 1 (or other wanted speed). You can alter the speed too by multiplying dx and dy with any value.

codeman

  • Jr. Member
  • **
  • Posts: 78
Re: Facing a mathematical problem..
« Reply #2 on: January 27, 2015, 11:24:42 pm »
User137  it worked.. Thanks  :)    (http://postimg.org/image/c07l2fjt1/)
« Last Edit: January 27, 2015, 11:26:41 pm by codeman »
Lazarus v2.02 Win10

 

TinyPortal © 2005-2018