[SOLVED]World Scrolling Issue

Hello, I'm still pretty new to as3 and especially new to WCK. I have managed to hack a plethora of tutorials together, and I'm confident I'm getting somewhere.

I've managed to get one of Emanuele Feronato's tutorials (Tutorial) to work with the WCK.

I'm trying to implement a panning feature, exactly like line rider, where the player holds down a key and can pan through their world. If myWorld.scrolling = true and myWorld's focus is attached to a movie clip already centered to the stage, everything is fine while drawing lines. The problem comes when I try to move myWorld, or myWorld's focus is on a mc that isn't placed in the center of the stage,and draw more lines. The lines still draw to the stage, but the boxes created have an offset that doesn't seem relative to the amount the view port of the stage has moved.

I feel like I understand enough of this code to be a little dangerous, but not enough to know if I need to simply change a couple variables with globalToLocal. (I have tried changing a few variables this way but have had no luck.)

Here's the code I'm using to draw:

Code:
 
public function mousePressed(e:MouseEvent):void {
            isDrawing = true;
            canvas.graphics.moveTo(mouseX, mouseY)
            pointsArray = new Array;
            savedX = mouseX;
            savedY = mouseY;
            pointsArray.push(savedX);
            pointsArray.push(savedY);
         }
         


canvas is a Sprite.

Code:
 
public function mouseMoved(e:MouseEvent): void {
            if(isDrawing) {
               var distX:int = mouseX - savedX;
               var distY:int = mouseY - savedY;
               if (distX * distX + distY * distY > pixelDist * pixelDist) {
                  canvas.graphics.lineTo(mouseX,mouseY);
                  savedX = mouseX;
                  savedY = mouseY;
                  pointsArray.push(savedX);
                  pointsArray.push(savedY);
               }
            }
         }


Code:
public function mouseReleased(e:MouseEvent):void {

         isDrawing=false;
         var sx:int;
         var ex:int;
         var sy:int;
         var ey:int;
         var dist_x:int;
         var dist_y:int;
         var dist:Number;
         var angle:Number;
         var segments:int=pointsArray.length/2-1;
         for (var i:int=0; i<segments; i++) {
            sx=pointsArray[i*2];
            sy=pointsArray[i*2+1];
            ex=pointsArray[i*2+2];
            ey=pointsArray[i*2+3];
            dist_x=sx-ex;
            dist_y=sy-ey;
            dist=Math.sqrt(dist_x*dist_x+dist_y*dist_y);
            angle=Math.atan2(dist_y,dist_x);
            createBox((sx+ex)/60,(sy+ey)/60,Math.abs(dist)/60,2/30,angle);
         }
         canvas.graphics.clear();
         canvas.graphics.lineStyle(5);
      }


Everything so far has been just about the same code as Emanuele's, but this next function I altered a bit to work with WCK.

Code:

   public function createBox(px,py,w,h,a):void {
            var box:Wall = new Wall;
            
            box.position =(px,py);
            box.x = px*30;
            box.y = py*30;
            box.width = w*60;
            box.height = h*60;
            box.rotation = a*rad2Deg;
            box.type = "STATIC";
            myWorld.addChild(box);

      }

Changing this function took me a while and I've been playing with the variables, but all of them need to be set to exactly what they are right now for the boxes to be created where the line was drawn.


I've looked through the forum and especially at burtonposey's BoundaryDemo but I haven't been able to solve this one.


Please let me know if I need to clarify further.
I'd be grateful for any help, or any push in the right direction.
I just skimmed through your post so this may or may not be relevant.

When using box2d, there are two related "worlds". One is the physics world and the other is the display world. The two are related with the world.scale property. Usually, default is 30 or 60. That seems to be the numbers that you are using in createBox function.

When the screen moves, you need to make sure you are using the right offset from using mouse in the display world to the physics world.
Thanks for the reply. Any idea on how I would go about implementing that into the code I've posted?

The problem may be that I'm drawing outside the bounds of the stage (defined by the document properties). When createBox happens it stays within the bounds of the stage. I'm not sure how to go about fixing this without changing the size of the stage.
I haven't looked into the scrolling yet but the issue may be that you are using mouseX, mouseY which doesn't take into account the panning.
Again, thanks for the reply.

What other coordinates could I use? I'm using the mouse/finger (when ported to mobile) to draw to the canvas.
BlakeBox wrote:
The problem comes when I try to move myWorld, or myWorld's focus is on a mc that isn't placed in the center of the stage

I would start with that mc's coordinates as reference.
Hell yes! Thanks so much for walking me through this one.

I have the world focused on an object and move it with the start and stop drag functions. I saved the delta coords of the focus object and add that to px and py in the createBox function.

It seems my problem wasn't as dramatic as I was making it out to be!
You're welcome 🙂

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