|
Post by joeschmoe on Aug 31, 2010 17:55:56 GMT -5
This must be sooo basic, but...
Anybody know how to move a sprite along a smooth path to another location? I know how to find the slope of the line between two points. What gives me trouble is reducing the numbers I get to create smooth motion.
Example: Gun shoots from Point A to Point B. Want shell to travel on something close to a straight line from one point to the other. I can make it work sometimes by making a loop and dividing repeatedly by 2 or 3 until both numbers are less than, say, 10 or 15. But, obviously flawed and doesn't always work, never very accurate and sometimes way off.
Any help or direction to help appreciated.
|
|
|
Post by Darkjester on Aug 31, 2010 18:10:53 GMT -5
post your code it will help us help you lol
|
|
|
Post by joeschmoe on Aug 31, 2010 18:40:41 GMT -5
DJ,
I wasn't sure if the code would help. The basic idea seems to be so flawed. Anyway...
After I get the slope of the line (using Y2-Y1 for the Y component, X2-x1 for the X component) I'm left with two numbers representing (I guess) the amount of X and Y I want to move the sprite to create, at least, the illusion of a smooth path.
If these numbers are like 70 and 9, I don't want to move my sprite by 70 and 9. That would look stupid. So I want to reduce them smaller numbers (slope as a fraction, then reduced, y'know) but same ratio. Eyeballing, 7 and 1 would work, but computer don't eyeball so well. So I used:
Do until ABS(SX1)<15 and ABS(SY1)<15 SX1=SX1/2:SY1=SY1/2 Loop
Generally, it kindasorta works, but not well when one number is really big. I guess that's obvious, right...divide and truncate eventually makes one X or Y=0 and, over long distance, that creates a huge error, miss the target.
I know this must be simple.
Thanks for your help.
PS- I could post whole game on Wiki space but it's a little raw.
|
|
|
Post by DJLinux on Sept 1, 2010 7:47:04 GMT -5
The name of this math function is "Lerp" x = a+ (b-a) * scale scale >=0.0 and <=1.0 You scale a value between a and b in fine steps of fade It works for 1D,2D, or 3D vectors function Lerp#(from#(),to#(),fade#) () dim result#(1) result# = from# +(to#-from#)*fade# return result# end function
dim scale#,newpos#(1),from#(1),to#(1) ' from x,y to x,y
from# = vec2(10,10) to# = vec2(20,40) for scale#=0 to 1.0 step 0.1 NewPos#=Lerp#(From#,To#,Scale#) printr "x=" + NewPos#(0) + " y=" + NewPos#(1) next with this you can move a thing on a strate line from pos A to pos B in any fine steps between 0.0 and 1.0 if you will move a space ship or a car on curved lines you need a cubic bezier function. A Bezier function descripts a path of way points with an optional value of turning/steering. You can move any thing more organic as on a strate line. Joshy
|
|
|
Post by joeschmoe on Sept 3, 2010 11:02:43 GMT -5
Thanks Joshy. Works perfectly.
|
|