Follow Interpolation?

Blitz3D Forums/Blitz3D Programming/Follow Interpolation?

Guy Fawkes(Posted 2012) [#1]
Hi, can someone please show a small, example code of how to make a 3D object follow a spline? And make it so the spline is drag and drop? Thank you so much! :)


Axel Wheeler(Posted 2012) [#2]
I don't have exactly what you are asking for, but "spline" usually refers to hermite/Catmull-Rom interpolation, rather than linear or cosine. I do have a function to perform the interpolation itself (below), but what you are asking for would require a significant amount of work (which is probably why nobody responded so far). I've never seen a blitz program do what you are talking about so you may be on your own there.

Still, here's the code to find a Catmull-Rom interpolated value between the middle two points in a series of 4 points:

Function catmullrom#(v0#,v1#,v2#,v3#,x#)
	v0#=0.5*(v2#-v0#)
	v3#=0.5*(v3#-v1#)
	
	;The above two lines apparently convert from regular Hermite interpolation (which isn't generally useful) to Catmull-Rom (which is).
	
	p#=2.0*(x^3.0)-3.0*(x^2.0) + 1.0
	q#=-2.0*(x^3.0)+3.0*(x^2.0)
	r#=x^3.0-2.0*(x^2.0)+x
	s#=x^3.0-x^2.0
	Return p*v1+q*v2+r*v0+s*v3
End Function


You need to feed it the values of 4 points, v0-v3, and the distance between v1-v2 that you want (x). This should give you a nice smooth curve. Good luck with the rest Rez!


Guy Fawkes(Posted 2012) [#3]
How would I simply make an object follow a catmullrom? i should have asked it that way... Sorry guys ><


Ginger Tea(Posted 2012) [#4]
i should have asked it that way... Sorry guys ><


TBH I've never heard of a Catmull-Rom* before, might have encountered it in some form or another, but not this fancy smansy name, a spline I have heard of, iir they are the vector drawing lines used in the likes of corel draw that you can curve.

In future (not just for you but others in general) I sugest posting a quick mspaint of what you want if anything is visual, like here is a curvy line that I want to follow.
And having a wiki link to any fancy words you or the repliers use

*Cat mulls ROM is what I read and expect someone to post a pic of a cat looking intently at a PCB


Guy Fawkes(Posted 2012) [#5]
LOL! XD

Anyway, its EXACTLY like a spline. Infact, in a way, I do believe it IS a spline.


Does anyone have an example of a SPLINE-like line whos "dots" or point a or point b, whatever u want to call them, with an object that goes say.... around in a small circle, and theres like a variable to stop it at either A OR B OR C OR D OR whatever?

And be able to like drag the point?

I think it would be useful not just for me, but for the community as a whole, because I think if beginners learn the HARDER stuff FIRST, they are MORE LIKELY to succeed when it comes down to that point, no pun intended. :)


If you are able to make it so u can drag a point and the spline will curve to the correct value, I MAY be able to make it write out each spline's position and what not, and make it so that everyone can create a sort of spline "waypoint" if u know what i mean, to use in their game for ANY OBJECT OF their choosing :)

Thanks! =D


Ross C(Posted 2012) [#6]
Beginners shouldn't learn the hard stuff first. They are more likely to just piece code together and not understand it, then ask questions and get answers they still don't understand, then just ask someone to code it for them. Completely defeats the point.

I don't entirely know how splines work and the interpolation between them. There definitely is examples in the code archives. I got some good code from there a couple of years back. Actually moving the points is the easy bit. It's just a set of co-ordinates. You asking essentially for someone to code a spline editor.

Anyways, you'll find the code you need to traverse a spline in the archives somewhere.


Axel Wheeler(Posted 2012) [#7]
I agree with Ross C.

For the movement part my first approach would be using a custom type called WayPoint or whatever. Each instance would have a pivot positioned along the spline. Then just iterate the type and update the location of your entity. Then PointEntity it to the next pivot.

For this to work you would need to know in advance the distance along the line it will move each update. Of course if this is interactive with the player then this approach won't work, since you won't have that information in advance.

So the long and the short is: There are too many unanswered questions to really be able to design the system for you, let alone code it.

If you describe for us what kind of game it is and what the game element(s) involved really need to do maybe we can be more help.

For example if the entity needs to smoothly move in all three dimensions and be able to curve back on itself, even my code above won't help you because it assumes even spacing in one dimension (it's a traditional f(x) function).


Guy Fawkes(Posted 2012) [#8]
Ok guys. I need some help with this. I've gotten a demo I found in the code archives to allow for dragging of single waypoints... PROBLEM is the "dots" as well as the path that the entity moves on is NOT parented to the selected mouse waypoint... So basically u can move a waypoint. but the objects wont automatically adjust to the new coordinates of the bezier path :(

I could REALLY use some help with this please and thank you. I added drag and drop to the waypoint, so u can see im at least TRYING :)

Here's the code:




Thanks! :)


Kryzon(Posted 2012) [#9]
Hi Rez, do you have an e-mail?


Guy Fawkes(Posted 2012) [#10]
Yes, I do, Kryzon. Why do u ask? :)


Axel Wheeler(Posted 2012) [#11]
Kryzon's email address is in his profile, so send him one if you don't want to give out your email here. He may want to tell you not to ask for a "small example" of a complex piece of software to be coded for you, or maybe he wants to tell you to credit Charrua's work above. :-)

My thougths on the problem:

It's not about "parenting" the dots to the waypoint because the relative positions will change.

Each time the user moves a waypoint you will probably have to delete and recreate the entire path. That is because the curve between two points depends on more than just those two points. So just create a new set of waypoints with the new waypoint position and create a new path based on those waypoints, then delete the old path/waypoints, and move any objects to the new path. This is likely to be reasonably quick, unless you are dealing with huge paths.

Also, this is probably easy since you can just call Charrua's existing functions (I haven't looked at them myself to confirm this, but dang it sounded good, didn't it?).

The question then is, Do you want to update the path while the user is dragging the waypoint, or just have it snap to the new position when the waypoint is released? You could would try it both ways and see how it runs. Good luck.


Charrua(Posted 2012) [#12]
hi, i found a post about that subject with a sample code that whould help:

http://blitzbasic.com/Community/posts.php?topic=85377#965678

following is the code:



basically any changes to one of the 4 poins derives y recalculate the intermediate points. The sample uses 3D objects as dots of the path, to visually see the effect.

Juan


Guy Fawkes(Posted 2012) [#13]
@Charrua, I modified your code, alls it needs is to get the dots to follow the waypoints depending on where each waypoint is dragged. and have the object that is ALREADY following the bezier waypoints to re-calculate its coordinates automatically when the user lets go of the mouse and has dragged the waypoint :)

If you could help, I would appreciate that! :)

And btw, sorry for not crediting u Charrua, I was in a hurry. Credit goes to Charrua for the creation of the Bezier Waypoint system! :) Thank you, Charrua! :)


Kryzon(Posted 2012) [#14]
I wanted to share some ideas with you Rez, but your e-mail is not in your profile. So if you could, please send something to mine so I get your address, or untick the "Hide e-mail address" option.

Bye!


Guy Fawkes(Posted 2012) [#15]
Sure, Kryzon! Not a problem! I will email u :)


Charrua(Posted 2012) [#16]
the interpolation part is resolved, you have to ask for "How to Drag an Object in 3D space with the mouse" or something similar.

see the following from the code archives, it was one of my first readings about interpolation:

http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=843

Juan


Guy Fawkes(Posted 2012) [#17]
Thanks, Charrua! :) Thats CLOSE. but for some reason, the waypoint wont stay ON the line as the line follows the waypoint. plus. i need this to be in 3D. the CLOSEST thing I have right now to what I need, is the awesome Bezier Object follower program u created :) If u could modify THAT to do EXACTLY what it does in the 2d version that u just posted, and make the object follow the newly dragged bezier coordinates, then this case will be SOLVED! :)

Thanks once again, Charrua, for ALL of your help! Thank you ALL for all of your help! :)

Kryzon, check ur email. I emailed u :)


Charrua(Posted 2012) [#18]
rez
there are 4 points, 2 are for control (most of the time outside the path) and 2 (first and end) wich the path pass through.

The path following system i wrote, creates the 2 control points based on the waypoints, so they are hidden for you, making it easy for any one wich wants to use this kind of interpolation.

the 2d version, simply use 2 keys for x and 2 keys for y, you only have to add 2 more keys for z and it is in 3d. Add some turn arround to the camera and you'll see that it is completely 3D.

juan


Charrua(Posted 2012) [#19]
here it is!



just a few new lines of code.


Guy Fawkes(Posted 2012) [#20]
Thanks, Charrua. I know THATS 3D but the one u JUST posted from jeppe nielsons thread was 2D. At any rate, do u think that u can make the bezier waypoints drag in YOUR 3D demo u made from AWC, and make the object follow ANYWHERE the bezier waypoint is placed?

Once again, thank you SO kindly for all of the help! :)


Charrua(Posted 2012) [#21]
you must try to Mix code samples to generata your own code. Most of you are asking for is on these forums, you only have to "learn" the technique and add some other pieces from here and there.

To drag an object you may do many things, it all depends on you style or needs. One way should be:

Use CameraProject to know ObjectX,ObjectY (2d on screen)
Get MouseX,MouseY and if near ObjectX,ObjectY (some threshold)
and if Left Mouse Button Pressed: get MouseX and Y speed and move the object
somewhat on x,y direction, try, pray, test, LEARN, see other code ( please don't ask someone else to code your work). There will be no one on this forum wich could make your wishes, this is your dutie. Here you will find so many help, but you must do some effort by your self.

regards

Juan


Guy Fawkes(Posted 2012) [#22]
Can someone else actually help step me through this please?


Guy Fawkes(Posted 2012) [#23]
Charrua, better idea. Do u think since AWC is freeware, that u could release the part that drags and drops the waypoints, and writes the waypoints data to a file? That would be EXACTLY what im looking for :)

It would be WONDERFUL of you if u could do that.. Why spend time re-inventing the wheel? :)


Axel Wheeler(Posted 2012) [#24]

Thats CLOSE. but for some reason, the waypoint wont stay ON the line as the line follows the waypoint.


The reason is that this was clearly the intended behavior, as it often is in interpolation implementations. You should not presume that he has the time to change it, since you have no idea how much work that would take. You should instead learn about splines from a variety of sources and do it yourself.

Do u think since AWC is freeware, that u could release the part that drags and drops the waypoints, and writes the waypoints data to a file?
...
Why spend time re-inventing the wheel? :)


Because everyone has to reinvent many wheels when they program computers. That's just the way it is. His Bezier code included routines to drag and drop waypoints, and if you have trouble writing code to save data to a file, start a thread on that (or better yet, search for one).

Limit your threads to one small request each.

As you yourself stated, Charrua has given you all the functionality you need between the two programs. You should now be able to extract what you need from both to make most or all of what you want. Why don't you give it a try and post your own code when you get stuck?


Guy Fawkes(Posted 2012) [#25]
I TRIED combining drag and drop with the bezier waypoints. it DID NOT work... the object wouldnt follow the new bezier waypoints new coordinates.....


Axel Wheeler(Posted 2012) [#26]
Then post your code and let these folks pick it apart.


Guy Fawkes(Posted 2012) [#27]
Ok, good point! :)

As Axel said, I'm limiting my post to 2 small requests each. 1. Can someone please make the "viajero" object follow the NEW coordinates to the dragged bezier waypoints? 2) Can someone make the plotted path change to the correct dragged bezier waypoint's coordinates?

Here is the code so far:



I've already added drag drop :)


Thanks alot for all of your help so far, and your help in the future! :)

Last edited 2012


Charrua(Posted 2012) [#28]
The waypoints entities are pivots, so they are invisible.
You are dragging cubes not associated in any way with the PathFollowingSystem.

The idea, as i said, depends on what your need, now and tomorrow. If you tomorrow wants to delete a waypoint or add waypoints, any explanations wich works today will start to fail.

As the path following sistem explain: you mus create a path, then add waypoints and then InitPath, then you can use Next/Previous to travel the path.

If you modify (or wants to) a waypoint, you have to: modify the x,y,z position of the Waypoint's entity and then recalculate the path (InitPath)

to do so, there are many techniques, first you can Link the Cube (that shows the position of the waypoint) to the waypoint in some way. I do it by naming the Cube with the handle of the WayPoint (then i get the object easily).

i do only 3 changes, 2 of them to link the cube to the waypoint object:
""

and after drag, to translate the new Picked entity position to the waypoint\entity



but... if you don't understand how the Path following system work, then any other changes will require other people intervention. Please try to use tools that you understand, or take the time to learn some technique before to start a new idea. I know that our imagination normally goes in another speed, but try to code and make real some basic ideas first. Know exactly what each one does and then, only then, try a new step.

(my english is terrible, hope you understand what i tried to say..)

Juan

Last edited 2012


Guy Fawkes(Posted 2012) [#29]
OMG! THIS IS PERFECT, CHARRUA! =D This is EXACTLY what I invisioned! And ur right about the delete thing. HOWEVER. BECAUSE I am creating a small editor WHICH I am releasing in the Code archives for everyone when it's finished, AND because it SAVES the NEW coordinates to a FILE and writes out an EXAMPLE of how to USE it, this is PERFECT and does NOT REQUIRE deletion of a waypoint! :)

Btw, ur english is JUST fine, I understand u enough to get the point! :)


Thanks alot, Charrua! :)


U shall be added to the credits! :) I CANNOT thank u enough! :)

EDIT: I DID notice a SMALL glitch. when u hold the mouse down, then move the mouse a little, the dots keep going o.o Other than that, its PERFECT! :)

Last edited 2012


Charrua(Posted 2012) [#30]
there should be some refinements

i call InitPaths() wich reinitialize All the paths defined, if you think it twice, it should be good to initialize only the path that contain the waypoint modified... if you use only one path this isn't necesary

when dragging it should be good to limit the axis on wich changes are made, or give the user the hability to select wich axis is edited... only two at a time or only one...


juan


Charrua(Posted 2012) [#31]
this "strange behavior" is related (i supose) not with InitPath, is related to the way the x,y,z position and orientation of the waypoint is done, for this is that i sugested to limit/select wich axis is edited. I saw that efect and as i see the x,y position on screen don't change but the z position is being modified and so, the path (visible dots).


Guy Fawkes(Posted 2012) [#32]
Ok. Ill add axis limiting in a bit. right now, ive found a few more things i would like to fix. :) I will do each of these problems, one step at a time.

1st problem is keeping the moving object at a constant speed no matter HOW stretched out the bezier waypoint path is :)


Charrua(Posted 2012) [#33]
ja ja
this is a subject for a tesis...

in the system i made, the amount of steps is defined when the Waypoint is created and independent of the distance of the path that follow the waypoints.

for this reason the speed is diferent, i saw some work's about this subject but one aproach should be, to calculate the distance of the path between each pair of waypoint and, then redefine the \nSteps acordingly

remember that each modification you made on the variables (stored on the tWayPont or another object) will be reflected after InitPath


Guy Fawkes(Posted 2012) [#34]
Here's what I have so far:




Charrua(Posted 2012) [#35]
i guess you missread or don't understood my #33 post.

if two waypoints are 200 blitz units distant from each other and another pair is 2000 (ten times) blitz units distant, making \nSteps constant implies that the distance between steps will be 10 times bigger.

if you need a constant speed you have to work on the subject a little more:
recalculate nSteps as a function of the distance between 2 adjacent waypoints (that guarantees constant speed between any pair of waypoint, but not between two interpolated points!)

as i said, that is a subject for a more mathematical aproach, far away of my path following system (and knowledge sorry)

i read somewhere (don't remember) that some one else work on a "constant speed between 2 interpolated points over a bezier curve"

Last edited 2012


Guy Fawkes(Posted 2012) [#36]
if its 3d, does anyone happen to know which thread that charrua is talking about? that is VERY useful information :)


Guy Fawkes(Posted 2012) [#37]
Does anyone know where I can find this post? :)


GitTech(Posted 2012) [#38]
http://www.blitzbasic.com/Community/posts.php?topic=96737#1121188


:-)



EDIT: I assume you mean the "#33 post" Charrua is refering to?

Last edited 2012


Guy Fawkes(Posted 2012) [#39]
No, I mean post #35 :)


Guy Fawkes(Posted 2012) [#40]
can anyone else help? :) I would greatly appreciate it! :)


Kryzon(Posted 2012) [#41]
Code Archives -> Algorithms -> CTRL+F to search for "constant" by Warpy.


Guy Fawkes(Posted 2012) [#42]
Awe crap >< This is 2D.. AND its blitzmax.... :/ Im not able to convert that to 3D OR Blitzbasic... :/





Guy Fawkes(Posted 2012) [#43]
Anyone? :/


Charrua(Posted 2012) [#44]
the translation to Blitz3D involve change the DrawText and DrawLine to Text and Line, "t1:+speed" to t1=t1+speed and "t2:+0.001" to t2=t2+0.001

KEY_ESCAPE is a constant with the value 1 (same for blitz)

the mathematical part don't need any changes. 3d only adds the z axis, try to compare the bezier interpolation warpy uses (in 2d) with the Interpolate_Pos in the waypoint system, you'll see that the Math part is the same, with a new z component.

as i said before, these forums has many nice pieces of code, assemble them to suit your needs is "your work", came on, nothing is going to happen without work. We can help, but no one work's for you.

BlitzMax Warpy's code translated to Blitz3D



Last edited 2012


Guy Fawkes(Posted 2012) [#45]
Isnt there a way u can modify ur bezier waypoint code?


RifRaf(Posted 2012) [#46]
wow


Blitzplotter(Posted 2012) [#47]
Verry Nice!