Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving sprite using certain path  (Read 3911 times)

0 Members and 1 Guest are viewing this topic.

SamuelGHOST

  • Newbie
  • *
  • Posts: 5
    • View Profile
Moving sprite using certain path
« on: November 24, 2018, 05:26:40 pm »
I have a vector that contains points. I already can move sprite from point A to point B, but I don't know how to move sprite from point A to point B, then to point C and so on. How to move sprite by multiple points that my vector have?

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Moving sprite using certain path
« Reply #1 on: November 24, 2018, 07:42:31 pm »
Move your sprite from A to B, and then from B to C.

I'd do it by adding a variable "last point reached", with the index in the vector of the point you started at (e.g. you start at A (0), go to B(1), then C(2)...)

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Moving sprite using certain path
« Reply #2 on: November 25, 2018, 09:48:50 am »
So basically learn Pathfinding.

SamuelGHOST

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Moving sprite using certain path
« Reply #3 on: November 25, 2018, 01:28:08 pm »
Move your sprite from A to B, and then from B to C.

I'd do it by adding a variable "last point reached", with the index in the vector of the point you started at (e.g. you start at A (0), go to B(1), then C(2)...)

The problem is that in "while loop" sprite instantly moves from one point to another. I want it to move smoothly to the next point.

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Moving sprite using certain path
« Reply #4 on: November 26, 2018, 07:36:36 am »
Who said anything about a while loop?


For example:
Say you are at A and your goal is B. Each game loop update, you would step towards B and then check if you are at B. Once you are at B, your goal is now C. Repeat.

What you are after is an array of points that you want the "sprite" to move towards (this would represent a path). Your array of points would contain B and C (A is obviously your starting point). You would iterate through each of the points in the array upon reaching the current point in the index (starting at 0).

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Moving sprite using certain path
« Reply #5 on: November 26, 2018, 07:01:07 pm »
Move your sprite from A to B, and then from B to C.

I'd do it by adding a variable "last point reached", with the index in the vector of the point you started at (e.g. you start at A (0), go to B(1), then C(2)...)

The problem is that in "while loop" sprite instantly moves from one point to another. I want it to move smoothly to the next point.

A point (A, B...) is just two coordinates.

Let's say A has coordinates [aX, aY] and B has coordinates [bX, bY]:

The point between A and B (let's call it C) would have for coordinates:
[(aX + bX) / 2, (aY + bY) / 2]

[ A - - - | - - - | - - - | - - - | ]
[ | - - - | - - - C - - - | - - - | ]
[ | - - - | - - - | - - - | - - - B ]

If you want to get the point (let's call it D) between A and B, at 25% (distance between the point D and A is 25% of the length between A and B), D would have for coordinates:
[(aX * 75 + bX * 25) / 100, (aY * 75 + bY * 25) / 100]

[ A - - - | - - - | - - - | - - - | ]
[ | - - - D - - - | - - - | - - - | ]
[ | - - - | - - - | - - - | - - - | ]
[ | - - - | - - - | - - - | - - - B ]

Now if you want ANY point between A and B, at x% (x is a number between 0 and 100, where 0 means exactly over A and 100 means exactly over B), you would use this formula to get those coordinates:
[(aX * {100 - x} + bX * x) / 100, (aY * {100 - x} + bX * x) / 100]

If you use this formula a lot, you can (and should) use x between 0 and 1, replacing every "100" with "1" and remove the "/ 100" so you do less operations.

So if you want to smoothly move from A to B, you'd use this formula at different percentages. You'll always have "teleportation" (instantly moving), but it will be so small that it'll look very smooth:



MORE ADVANCED MATHS WITH SMOOTH CURVES
(click to show/hide)
« Last Edit: November 26, 2018, 07:31:58 pm by ZeroZ30o »

 

anything