PDA

View Full Version : [AS3] Creating custom bezier curves from player movement


neondevil
06-26-2009, 11:52 AM
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)


target_angle = Math.atan2(distance_y,distance_x);

player_velocityX = Math.cos(target_angle)*rate;
player_velocityY = Math.sin(target_angle)*rate;


The only equation I know that will get me a 3rd point is:

getting curve distance = ((x1*x1) + (x2*x2))/2
//Repeat for y


But that will only give me a uniform curve every time, I need it to be specific to the movement.

Are there any formulas I can apply that will allow the object to mimic the gesture of the user?

Thanks

neondevil
06-28-2009, 08:50 PM
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?



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);

rrh
06-29-2009, 05:23 PM
I think you can get decent bezier curves just by alternating points with handles.

Imagine you take the position of the mouse for every frame. So, like you have an array of points, like [A, B, C, D, E, F, G, H] Then when making a curve, you use A as the point co-ordinates, and B as the bezier handle. Next, use C and D.