Game math (distance increments)

This is a discussion on Game math (distance increments) within the Game Programming forums, part of the General Programming Boards category; I have 2 points (start, end) represented by POINTS (ptStart, ptEnd). The 2 points represent the barrel of the players ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Game math (distance increments)

    I have 2 points (start, end) represented by POINTS (ptStart, ptEnd). The 2 points represent the barrel of the players gun and the target, respectively (it's a 2d iso game).

    I know that to get the distance between the points I would use:

    distance = sqrt(((ptEnd.x-ptStart.x)*(ptEnd.x-ptStart.x))+((ptEnd.y-ptStart.y)*(ptEnd.y-ptStart.y)))

    What I don't know is how to figure the incremental amounts added/subtracted to the original point to move the object to the end point. Unless my end point is and equal distance from my start x AND y, I can't just increment through the distance.

    What I'm looking for is a smooth transition from point a to point b. I just don't know what to add/subtract in my loop to get it.

    Thanks in advance for any help you can give.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Lets see if I can explain this..I'll give it a try ask me again if I don't get it clear

    Y = KX + M (Standard linear equation)

    deltaY = EndY - StartY
    deltaX = EndX - StartX

    K = deltaY / deltaX

    You know know the K value for your line. We now need to know M

    M = Y - KX (Standard linear equation)
    M = StartY - K*StartX

    You can now increment your X value from start to end and get the Y value for your linear curve by using Y =KX + M

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    That doesn't work (unless I misunderstood something, which is quite possible).

    Here's the code:

    Code:
    POINT ptStart, ptEnd, ptMob;
    
    DeltaX = ptEnd.x - ptStart.x
    DeltaY = ptEnd.y - ptStart.y
    K = DeltaY / DeltaX
    M = ptStart.y - K * ptStart.x
    Y = K * ptStart.x + M
    ptMob.x = ptMob.x + 1
    ptMob.y = ptMob.y + K
    With the following initial settings:

    ptStart.x = 5
    ptStart.y = 200
    ptEnd.x = 200
    ptEnd.y = 20
    ptMob.x = ptStart.x
    ptMob.y = ptStart.y

    It gives me this:
    DeltaX = 195
    DeltaY = -180
    K = -1
    M = 205
    Y = 200

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    -180/195 is not -1. You can't use int. cast to double. The method I oulined will also give you problem with perfectly vertical and horizontal lines so you have to test for those two cases if(DeltaX == 0 || DeltaY == 0) before you solve the linear equation

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    If you look at his math for a second you will realize that it is the old y=mx+b from school.


    A line is a linear interpolation between y and y2 and a linear interpolation between x or x2 or a bi linear interpolation of x,x2,y,y2.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Is there a better way to do this? I don't like the way that it works with vertical lines. It feals like a kludge.

  7. #7
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Barjor
    [B]-180/195 is not -1. You can't use int. cast to double.
    I'm using 2d coords (screen x,y). Are you saying to cast to double and then convert and use just the int portion? Would that give accurate enough results?


    *kicks self*
    Now I wish I would have paid better attention in high school (unless I did and I'm just too old to remember!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21