PDA

View Full Version : [AS3] tower defence game help w/ Bezier curve


kramcomposer
06-21-2009, 01:14 AM
trying to make a tower defense game


during game play, I want the user to be able to add points to the main path, and then drag those points, to make a bezier curve like path.

then of course, have the Enemies follow this path way.

During the game I need to slow down Enemies too.

And because the length of the line will increase by adding more points

I have no idea how to accomplish this. Any suggestions

attunedesigns
06-21-2009, 02:17 AM
Hiya-- try this: http://labs.zeh.com.br/blog/?p=104

It is the developer of the Tweener library talking about bezier points, with some nice demos.

kramcomposer
06-21-2009, 02:40 AM
That will help alot thanks.

My only problem now is that most of the tweening engines out there use a tween from point A to point B is X Seconds.

I am kinda hoping that there is a way that I can use a set movement speed.
IE Set an objects speed to 5, and it will only move 5 units a second towards the target along the path.

hopefully that makes sense :confused:

kramcomposer
06-21-2009, 02:16 PM
I think I can pull from the plugins create line, because it seems to make that sub matrix, still tryin to figure it out though...

attunedesigns
06-23-2009, 02:49 PM
haven't forgot about yer constant velocity, but here's another one i had: http://labs.hellokeita.com/2009/03/12/multi-bezier-curve/

Source files at the bottom

attunedesigns
06-25-2009, 01:57 AM
Well! Ive pondered a bit, and figured that (maybe) by detecting the velocity of the moving object and dynamically setting Tweener.setTimeScale to an scaled inverse of that.... you can keep the velocity in a certain range. NOW, as to how to do that, i dont have the math power yet. But here's what i did up quickly:


import caurina.transitions.*;
import caurina.transitions.properties.*;
CurveModifiers.init();

a.x=a.y=0;
var prevPt:Point=new Point(a.x,a.y);
var currPt:Point=new Point(a.x,a.y);
var endPt:Point=new Point(100,100);
var totalDist:Number=0;
var time:Number=5;
var timeConst:Number=5;
var speed:int=10; //constant; higher=faster
var path:Array=[{x:500,y:100},{x:200,y:20},{x:450,y:400},{x:20,y:2 00},{x:400,y:200}];
var path2:Array=[{x:200,y:20},{x:450,y:400}];
Tweener.addTween(a,{time:time,x:endPt.x,y:endPt.y, _bezier:path2,transition:'linear',onUpdate:trackVe l});

function trackVel():void{
currPt=new Point(a.x,a.y);
var xvel:Number=Math.abs(currPt.x-prevPt.x);
var yvel:Number=Math.abs(currPt.y-prevPt.y);
var vel:Number=xvel+yvel;
//var targetTimeScale;
//if (vel>10) targetTimeScale=(Tweener._timeScale-.05);
//else targetTimeScale=(Tweener._timeScale+.05);
//Tweener.setTimeScale(targetTimeScale);
trace(vel,'\n',Tweener._timeScale,'\n------------');
prevPt=new Point(a.x,a.y);
}

Approaching it from the other end would be determining the path distance of the bezier curve. Which involves considerably more math! I attempted this way too, by running the tween twice. The first quickly traces the path and aggregates the distance traveled (object not visible at this point). The second is the real run, wherein the time is calculated according the distance with a constant speed.

Oi. I know someone can get the math done on this...?

kramcomposer
06-30-2009, 09:46 PM
I got it working!

ill post the engine soon.

I couldn't have done it without your help

attunedesigns
06-30-2009, 09:50 PM
Nice! No problem.