Problems with custom bezier curves/gestural movement
Some background before I get into it. I'm building a game where the player can flick an object around the screen crashing into other objects.
All the object is doing right now is moving in straight lines and it goes by pushing the mouse button down then moving to another point and letting it go then it takes on the speed of that and the angle.
The problem is that it only moves in straight lines, I want to give the player the ability to curve the object. Then the object would conform to that curve depending on the velocity so say that you're running fast you wouldn't be able to make a sharp turn quickly. (I've pasted a bit of the code below on how it moves and I've attached the whole program, it's in CS3)
Ok I made a smaller version. The problem is my formula for finding the middle point is really skewed so it goes of screen. Even so this will still just get the same middle point and not really line up with the gesture.
Any ideas guys?
Code:
import caurina.transitions.*;
import caurina.transitions.properties.CurveModifiers;
CurveModifiers.init();
var coordinate_array:Array = new Array();
var bezier_x:Number;
var bezier_y:Number;
function main(e:Event):void{
trace(bezier_x);
if(coordinate_array.length >= 4){
bezier_x = ((coordinate_array[2]*coordinate_array[2]) + (coordinate_array[0]*coordinate_array[0]))/2
bezier_y = ((coordinate_array[1]*coordinate_array[1]) + (coordinate_array[3]*coordinate_array[3]))/2
//Ok now how do I get that dynamic middle point?
Tweener.addTween(thing, {x:coordinate_array[0], y:coordinate_array[1], _bezier:{x:bezier_x, y:bezier_y}, time:1, transition:"easeoutquad", onComplete:clearstuff()});
}
}
function clearstuff():void{
coordinate_array = [];
}
function mouse_down(MouseEvent:Event):void{
thing.x = mouseX;
thing.y = mouseY;
coordinate_array.push(mouseX,mouseY);
}
function mouse_up(MouseEvent:Event):void{
coordinate_array.push(mouseX,mouseY);
}
stage.addEventListener(Event.ENTER_FRAME, main);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down);
stage.addEventListener(MouseEvent.MOUSE_UP, mouse_up);
Ok I'm looking at the C code for it and I'm pretty confused. I'm assuming the pos array are my mouse points and that I'll have to convert my angle into degrees.
Is desiredPos the end point of the curve?
Is reVal my velocity?
Thanks for the help.
Code:
float lagrangeInterpolatingPolynomial (float pos[], float val[], int degree, float desiredPos) {
float retVal = 0;
for (int i = 0; i < degree; ++i) {
float weight = 1;
for (int j = 0; j < degree; ++j) {
// The i-th term has to be skipped
if (j != i) {
weight *= (desiredPos - pos[j]) / (pos[i] - pos[j]);
}
}
retVal += weight * val[i];
}
return retVal;
}
this expression may be used for your curved path formula coordinates at any moment of time. the degree has nothing to do with angle, it is polynomial degree, or length of pos and val arrays. desiredPos could be current time or frame count, retVal could be one of mouse coordinates; pos and val are corresponding things for mouse "gesture" data. in case of pos.length == val.length == 2 you obviously have a line passing through two endpoints, 3 gives you quadratic curve, 4 - cubic curve, etc.
this expression may be used for your curved path formula coordinates at any moment of time. the degree has nothing to do with angle, it is polynomial degree, or length of pos and val arrays. desiredPos could be current time or frame count, retVal could be one of mouse coordinates; pos and val are corresponding things for mouse "gesture" data. in case of pos.length == val.length == 2 you obviously have a line passing through two endpoints, 3 gives you quadratic curve, 4 - cubic curve, etc.