Struggling with beziers

Blitz3D Forums/Blitz3D Programming/Struggling with beziers

Ross C(Posted 2009) [#1]
Hey, i'm trying to get my entity to pass though waypoint, but i'm trying to use a bezier curve to do this. I tired using Stevie G's function he gave me for colour change, and slightly modifed it:

Function move_interpolate( t#, p1,p2,p3,p4)

	x1# = EntityX(p1,True)
	y1# = EntityY(p1,True)
	z1# = EntityZ(p1,True)
	
	x2# = EntityX(p2,True)
	y2# = EntityY(p2,True)
	z2# = EntityZ(p2,True)

	x3# = EntityX(p3,True)
	y3# = EntityY(p3,True)
	z3# = EntityZ(p3,True)
	
	x4# = EntityX(p4,True)
	y4# = EntityY(p4,True)
	z4# = EntityZ(p4,True)
	
	bezier_x = x1 * (1-t)^3 + 3 * x2 * (1-t)^2 * t + 3 * x3 * (1-t) * t^2 + x4 * t^3 
	bezier_y = y1 * (1-t)^3 + 3 * y2 * (1-t)^2 * t + 3 * y3 * (1-t) * t^2 + y4 * t^3 
	bezier_z = z1 * (1-t)^3 + 3 * z2 * (1-t)^2 * t + 3 * z3 * (1-t) * t^2 + z4 * t^3 
	
End Function


It basically takes the four pivots and extracts the x,y,z data. Now the main problem i have here, is the 2 and 3 points seem to act as control points, rather than go nodes for the curve to travel through. I have looked at some code archive examples... Frankly all the code archive entries that do this, are poorly documented or use one letter variable names.

Can anyone help?


Nate the Great(Posted 2009) [#2]
I have never tried using beizers for this and they seem to complicate the problem so I suggest calculating this stuff in real time ie. have the player have some kind of acceleration and velocity. then make it always turn toward the next waypoint.


Charrua(Posted 2009) [#3]
hi
efectively points 2 and 3 are control points, if they are aligned with points 1 and 4 (start and end) you get a stright line.

t is the amount of interpolation between P1 and P4 (from 0.0=P1 to 1.0=P4)
and he position of P2 and P3 (relative to the line formed by P1 and P4) modify the curve style you get

Better to see in action:



not so commented as you ask for but an image worth more than thousand words!


hope that help

Juan


_PJ_(Posted 2009) [#4]
Yes, Charrua's right. As far as I understand Bezier curves, out of a total of 4 points, only P1 and P4 lie on the curve (actually at each end). Points P2 and P3 define the 'shape' and size of the curve.

There's a diagram here that shows the relationships between the points, hope it helps:

Note.. At first, ignore all the other stuff and just note the P1,P2,P3 and P4 points in the first diagram.

http://www.efg2.com/Lab/Graphics/Jean-YvesQueinecBezierCurves.htm


Stevie G(Posted 2009) [#5]
In order to get smooth movement between alot of waypoint you need to position the control points correctly.

For example, say you had 4 waypoint P1-P4 and your player was to move between P2 and P3. The 4 bezier points would then be :

NP0 = P2
NP1 = P2 + ( P2-P1 ) * .33
NP2 = P3 + ( P3-P4 ) * .33
NP3 = P3

Notice how P1 and P4 influence the control points NP1 & NP2. The * .33 is just an example, you can play with this.

Make sense?

Stevie


Warpy(Posted 2009) [#6]
If you just want to make sure your entity goes through the waypoints and follows a curved path, Catmull-Rom splines might be easier to use. Unlike beziers, they don't have the extra control points defining the gradient, so you just give it the set of waypoints you want the curve to go through and it can interpolate between them.

I wrote some blitzmax code to demonstrate them a while ago, it shouldn't be too hard to port to b3d.


Kryzon(Posted 2009) [#7]
You know Warpy, now that you speak of it, Catmull-Rom does seem to be a much better spline method for waypoints.

You can see more info here: http://www.mvps.org/directx/articles/catmull/

EDIT: Now thinking about it, Cardinal Splines look to be simpler...oh, so many to choose from.

EDIT2: More info on interpolation methods:
http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/interpolation/


Naughty Alien(Posted 2009) [#8]
..to make things simple why not just create simple function what will gradually start turning your character towards next waypoint when reach within specific range near current one and so on. Its supersmooth it doesnt require any specific algorithm to nicely curve your character path and its really working well no matter how extreme is angle between two waypoints...


John Blackledge(Posted 2009) [#9]
Thi is what you want:
http://www.blitzbasic.co.nz/toolbox/toolbox.php?tool=203


Ross C(Posted 2009) [#10]
Thanks guys, i will look into all these options. Th ereason i'm not too keen on just smoothly pointing the entity, is because it's a tower defence game, and the path is already set. I'd rather see in curve form, where my enemy will go, so i can adjust the path if need be. And it saves putting down too many waypoints :)

Thanks again!


Nate the Great(Posted 2009) [#11]
it's a tower defence game


oh that explains why you wouldnt want to turn the enemies toward the next waypoint smoothly.


Ross C(Posted 2009) [#12]
Sorry to bump this back up, but thanks again John for the link. That curve stuff is awesome. So easy to use.


Guy Fawkes(Posted 2009) [#13]
how do u create a waypoint? createpivot?


Ross C(Posted 2009) [#14]
How ever you wish. A waypoint is just a set of co-ords. In blitz, is helpful to place a pivot at the co-ords you want to be a waypoint, so you can use commands such as point entity. The curve lib however, only uses co-ords.


Guy Fawkes(Posted 2009) [#15]
ah i see. thanks ross! :)


Charrua(Posted 2010) [#16]
just in case you still are interested, take a look to:

http://blitzbasic.com/codearcs/codearcs.php?code=2675#comments

Juan