Continuous line inside the world

Hi,

I am using box2d wck for a task, i need to draw a continuous line , like here -http://www.freewebarcade.com/game/draw-a-line/

If you see , the once the line is drawn when the mouse is pressed down , it draws from the point where the last point ended ....

I have been able to draw a line but not continuous , if i do mouseup and draw again i can draw a separate line,but i require line to be continuous ....

I have tried and tweaked all the 3 classes including trying to change the point values , but it does not seem to work .....

Here are the 3 main classes from wck - the Crayon class --


////////======================
package extras {

import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import wck.*;
import misc.*;
import flash.utils.*;
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.geom.*;
import flash.ui.*;

/**
* Base crayon class - doesn't create any shapes.
*/
public class Crayon extends BodyShape {

public var lineThickness:Number = 8;
public var lineColor:Number = 0xFFFFFF;

var tracer:XYTracer;

var source:XYSource;

public override function create():void {
type = 'Static';
super.create();
tracer = new XYTracer(source);
tracer.addPoint(new Point(0, 0), false);
tracer.localTo = this;
listenWhileVisible(tracer, XYTracer.TEMP_POINT, onTempPoint);
listenWhileVisible(tracer, XYTracer.PERM_POINT, onPermPoint);
listenWhileVisible(tracer, XYTracer.FINISHED, onFinished);

}

public function onTempPoint(e:Event):void {
onPoint(e);

}

public function onPermPoint(e:Event):void {

onPoint(e);
}

public function onPoint(e:Event):void {
graphics.clear();
graphics.lineStyle(lineThickness, lineColor);
graphics.drawGraphicsData(Vector.<IGraphicsData>([tracer.path]));
}

public function onFinished(e:Event):void {
listenWhileVisible(world, StepEvent.STEP, updateBodyMatrixSimple, false, -10);

}
}
}

/////////////////////======================================================

The CrayonLine Class -

///////////////////======================

package extras {

import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import wck.*;
import misc.*;
import flash.utils.*;
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.geom.*;
import flash.ui.*;

public class CrayonLine extends Crayon {

public var tempShape:b2Fixture = null;

[Inspectable(defaultValue='')]
public var tipName:String = '';

public var tip:BodyShape;
/////////////============

///////////////============
public override function create():void {

var s:XYSourceDisplayObject = new XYSourceDisplayObject();
source = s;
super.create();
if(!tip && tipName != '') {
tip = Util.getDisplayObjectByPath(this.parent, tipName, world) as BodyShape;


}
if(!tip ) { /// Assume this was added as a result of mouse down.

tip = new CrayonTip();
tip.reportBeginContact = true;
Util.addChildAtPosOf(world, tip, this);


}
listenWhileVisible(tip, ContactEvent.BEGIN_CONTACT, handleTipContact)
listenWhileVisible(tip, Event.REMOVED_FROM_STAGE, handleTipRemoved);
s.obj = tip;
}

public function handleTipRemoved(e:Event):void {
tracer.finish();
}

public function handleTipContact(e:ContactEvent):void {
if(e.other.m_userData == this) {
//e.contact.SetSensor(true);
e.contact.SetEnabled(false);
}
}

public override function onTempPoint(e:Event):void {
super.onTempPoint(e);
createTempShape();
}

public override function onPermPoint(e:Event):void {
super.onPermPoint(e);
createTempShape();
var f:b2Fixture = addShapeForPoints(tracer.points[tracer.points.length - 2], tracer.points[tracer.points.length - 1]);
}

public override function onFinished(e:Event):void {
super.onFinished(e);
destroyTempShape();
//b2body.SetType(b2Body.b2_dynamicBody);
b2body.SetType(b2Body.b2_staticBody);
}

public function createTempShape():void {
destroyTempShape();
if(tracer.tempPoints.length > 0) {
tempShape = addShapeForPoints(tracer.points[tracer.points.length - 1], tracer.tempPoints[tracer.tempPoints.length - 1]);
}


}

public function destroyTempShape():void {
if(tempShape) {
tempShape.destroy();
b2fixtures.pop();
tempShape = null;
}
}

///////////////===========
//var p1:V2 = V2.fromP(point1);
// var p2:V2 = V2.fromP(point2);
// var dif:V2 = V2.subtract(p2, p1).divideN(2);
// var pos:V2 = V2.add(p1, dif);
/////////////////////////////

public function addShapeForPoints(point1:Point, point2:Point):b2Fixture {
var p1:V2 = V2.fromP(point1);
var p2:V2 = V2.fromP(point2);
//var dif:V2 = V2.subtract(p2, p1).divideN(2);
var dif:V2 = V2.subtract(p2, p1).divideN(2);
var pos:V2 = V2.add(p1, dif);

initFixtureDef();
var b:b2Fixture = box(dif.length() * 2, lineThickness, pos, dif.angle() * Util.R2D);



return b;


}
}
}

//////////////////////////============================================


And the final crayontip class -

///////////=========================

package extras {

import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import wck.*;
import misc.*;
import flash.utils.*;
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.geom.*;
import flash.ui.*;
import shapes.*;

public class CrayonTip extends Circle {

public var radius:Number = 2.5;
public var color:Number = 0xffffff;

public override function create():void {
graphics.drawCircle(0, 0, radius);
bullet = true;
super.create();
world.createDragJoint(this);
listenWhileVisible(stage, Input.MOUSE_UP_OR_LOST, remove);
}
}
}

////////////////////////======================================


can any one please guide me where to make the change ...

Thanks in advance ....

Jin
That's probably too much code for most people to bother reading. If you could describe the method in more general terms I think it would be easier to discuss.
For example, this is how I have done it in previous projects:

Code: Select all

On touch/mouse down {
    Record the starting point as P
}

On touch/mouse moved {
    If the current location of the touch/mouse is further from P than some threshold {
        create a line between the current location and P
        set P to the current location
    }
}
@irrestibleforce - you are rights my bad ,this is the main class -

Now my main issue here is that i know where the point is getting created and every thing and i also try to change code but sometimes the line start getting drawn from (0,0) and i am not able to stop a separate tip being created ......

Its a great extension developed by mayobutler but unfortunately due to my own fault , i am finding it hard to understand where to change ...

Can you guide me where i need to make the changes which might work .....

thanks again ....

//////////====================

package extras {

import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;
import cmodule.Box2D.*;
import wck.*;
import misc.*;
import flash.utils.*;
import flash.events.*;
import flash.display.*;
import flash.text.*;
import flash.geom.*;
import flash.ui.*;

public class CrayonLine extends Crayon {

public var tempShape:b2Fixture = null;

[Inspectable(defaultValue='')]
public var tipName:String = '';

public var tip:BodyShape;
/////////////============

///////////////============
public override function create():void {

var s:XYSourceDisplayObject = new XYSourceDisplayObject();
source = s;
super.create();
if(!tip && tipName != '') {
tip = Util.getDisplayObjectByPath(this.parent, tipName, world) as BodyShape;


}
////////////////deduct mouse down
if(!tip ) { /// Assume this was added as a result of mouse down.

tip = new CrayonTip();
tip.reportBeginContact = true;
Util.addChildAtPosOf(world, tip, this);


}

listenWhileVisible(tip, ContactEvent.BEGIN_CONTACT, handleTipContact)
listenWhileVisible(tip, Event.REMOVED_FROM_STAGE, handleTipRemoved);
s.obj = tip;
}

public function handleTipRemoved(e:Event):void {
tracer.finish();
}

/////////////on contact with canvas

public function handleTipContact(e:ContactEvent):void {
if(e.other.m_userData == this) {
//e.contact.SetSensor(true);
e.contact.SetEnabled(false);
}
}


creating the points

public override function onTempPoint(e:Event):void {
super.onTempPoint(e);
createTempShape();
}

public override function onPermPoint(e:Event):void {
super.onPermPoint(e);
createTempShape();
var f:b2Fixture = addShapeForPoints(tracer.points[tracer.points.length - 2], tracer.points[tracer.points.length - 1]);
}


public override function onFinished(e:Event):void {
super.onFinished(e);
destroyTempShape();
//b2body.SetType(b2Body.b2_dynamicBody);
b2body.SetType(b2Body.b2_staticBody);
}


////////////////creating shapes
public function createTempShape():void {
destroyTempShape();
if(tracer.tempPoints.length > 0) {
tempShape = addShapeForPoints(tracer.points[tracer.points.length - 1], tracer.tempPoints[tracer.tempPoints.length - 1]);
//tempShape = addShapeForPoints(tracer.points[tracer.points.length - 1], tracer.tempPoints[tracer.tempPoints.length - 1]);
}



}

public function destroyTempShape():void {
if(tempShape) {
tempShape.destroy();
b2fixtures.pop();
tempShape = null;
}
}

///////////function for shapes

public function addShapeForPoints(point1:Point, point2:Point):b2Fixture {
var p1:V2 = V2.fromP(point1);
var p2:V2 = V2.fromP(point2);
//var dif:V2 = V2.subtract(p2, p1).divideN(2);
var dif:V2 = V2.subtract(p2, p1).divideN(2);
var pos:V2 = V2.add(p1, dif);

initFixtureDef();
var b:b2Fixture = box(dif.length() * 2, lineThickness, pos, dif.angle() * Util.R2D);



return b;


}

}
}

/////////////////==================================================

Regards
Jin
Still too much code for me I'm afraid, and I'm not familiar with Flash or WCK anyway. I can't even tell if this code is following the method I outlined in pseudocode.
Isn't there TouchDown, TouchMoved, TouchEnded functions somewhere to isolate the behavior in question?
@irrestibleforce - thanks for the time being i have disable the line draw once the mouse is up , its not a perfect solution but will force user to draw line at one go ...

Thanks again ..

Regards
Jin

Original thread: https://box2d.org/forum/viewtopic.php?t=9824