A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Problems with custom bezier curves/gestural movement

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    21

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


    Code:
    	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:

    Code:
    			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
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    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);

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    sigh, I am greatly depressed over TPB joining Napster fate to code anything atm, so here go few links you will have to read yourself

    simple read and tough read
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Quote Originally Posted by realMakc View Post
    sigh, I am greatly depressed over TPB joining Napster fate to code anything atm, so here go few links you will have to read yourself

    simple read and tough read
    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; 
    }

  5. #5
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    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.

    edit: see an example here
    Last edited by realMakc; 07-01-2009 at 02:20 AM. Reason: link to example
    who is this? a word of friendly advice: FFS stop using AS2

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Quote Originally Posted by realMakc View Post
    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.

    edit: see an example here
    You sir are a gentleman and a scholar thanks alot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center