Box2D Forums

It is currently Wed May 22, 2013 5:33 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Sat Dec 04, 2010 8:35 pm 
Offline

Joined: Mon Nov 29, 2010 7:18 pm
Posts: 7
Hi there,
I would like to modify the platform game demo that comes with WCK to follow a camera instead of the boxman 'player'

I have set up a camera class (extends sprite) and i have an instance of it on stage called 'cameraObj'

On the WorldGame component i have set focusOn to 'cameraObj' and i can now manipulate my camera and WCK follows it nicely, keeping the parallax effect.

my main issue is that rotating or scaling the camera has no effect, it seems that focusOn just responds to x/y coords.

any ideas on how i could go about implementing rotating and scaling of the World from within my Camera class? I realise there are a lot of camera/panning/rotating/zooming questions on here but in the case of WCK a system has already been implemented so I was hoping for some advice on how to work with that.

thanks for your help
-D


Top
 Profile  
 
PostPosted: Sun Dec 05, 2010 12:09 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Extend wck.World and override the scrollRotation() method. This function is actually defined in the base class, misc.Scroller. wck.World overrides it to rotate the world based on gravity, but you can override it again and implement rotation different. You can do scaling there too.

It should look something like this (I haven't tested, should work in theory):

Code:
public override function scrollRotation():Number {
    scaleX = focus.scaleX;
    scaleY = focus.scaleY;
    return focus.rotation;
}


Top
 Profile  
 
PostPosted: Sun Dec 05, 2010 5:22 pm 
Offline

Joined: Mon Nov 29, 2010 7:18 pm
Posts: 7
thanks so much for your help, that worked a treat :) at first i was trying to extend wck.World and override scrollRotation in my Camera class and it wasn't working, but then i made my own World class DWorld that extends wck.World and set the WorldGame base class to point to DWorld instead of wck.World. Overriding the scrollRotation function in DWorld has done the trick :)

thanks for all your wonderful work, it's much appreciated, loving WCK

-D


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 3:10 am 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
Hmmm, I modified misc.Scroller to add easing. I don't generally like to touch library class files directly, and now wonder if I should've overriden wck.World as you suggest.
For what it's worth, here are the changes I made-- one new component property and one modified function...

Code:
[Inspectable(defaultValue=0.2)]
public var scrollEase:Number = 0.2;

...

public function updateScroll(e:Event = null):void {
   /// If we have a focus object, then find the new position of the focus object in the viewport.
   if(focus) {
      pos = Util.localizePoint(this, focus);
   }
   var p:Point = pos.clone();
   rot = scrollRotation();
   var r:Number = rot;
   /// If mid-focus-change, tween from the old position & rotation to the new one.
   if(tFunc != null && tPos != null) {
      if(++tFrames == tFramesTot) {
         tFunc = null;
      }
      else {
         r = tFunc(tFrames, tRot, Util.findBetterAngleTarget(tRot, r) - tRot, tFramesTot);
         p.x = tFunc(tFrames, tPos.x, p.x - tPos.x, tFramesTot);
         p.y = tFunc(tFrames, tPos.y, p.y - tPos.y, tFramesTot);
      }
   }
   /// Set the position and rotation of the viewport.
   rotation = r;
   var oldX:Number = x;
   var oldY:Number = y;
   x = 0;
   y = 0;
   p = localToGlobal(p);
   x = oldX + ((stage.stageWidth / 2 - p.x) - oldX) * scrollEase;
   y = oldY + ((stage.stageHeight / 2 - p.y) - oldY) * scrollEase;
   /// Nudge the viewport based on the mouse.
   if(Input.mouseDetected) {
      //x += (stage.stageWidth / 2 - Input.mousePos.x) * (tFunc != null ? tFunc(tFrames, tMX, mouseNudgeX - tMX, tFramesTot) : mouseNudgeX);
      //y += (stage.stageHeight / 2 - Input.mousePos.y) * (tFunc != null ? tFunc(tFrames, tMY, mouseNudgeY - tMY, tFramesTot) : mouseNudgeY);
   }
   x = Math.round(x);
   y = Math.round(y);
   if(shake) {
      x += shake.x;
      y += shake.y;
      shake.normalize(shake.length * -0.5);
      if(shake.length < 1) {
         shake = null;
      }
   }
}


By the way Jesse, I hope you're okay with WCK getting its own forum. It's an excellent engine, and I think it's well deserved. :)


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 5:33 am 
Offline

Joined: Mon Nov 29, 2010 7:18 pm
Posts: 7
Barliesque wrote:
Hmmm, I modified misc.Scroller to add easing. I don't generally like to touch library class files directly, and now wonder if I should've overriden wck.World as you suggest.
For what it's worth, here are the changes I made-- one new component property and one modified function...



I was going to do what you did too ;) I guess jesses way is a little cleaner should you want to update wck in the future

you can still add an inspectable parameter to the class you create that extends World and it will show up in the parameters list along with everything else. My world component now has an 'orientToFocus' param included in it without having changed any library files.


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 5:11 pm 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
Quote:
I guess jesses way is a little cleaner should you want to update wck in the future

Exactly. I think I'll change that.

Quote:
you can still add an inspectable parameter to the class you create that extends World and it will show up in the parameters list along with everything else

Yeah, I know, although I tried doing that with something else, and for some reason the added parameter wouldn't show up. Can't remember now what I was doing-- I'll try again.

Come to think of it, I think I'd like to add some alternative targeting functionality like you've described. Rather than a special Camera class, though, I think I'd pass a generic DisplayObject that's on stage as the target. What's in your Camera class that would work differently? --if you don't mind my asking.


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 5:38 pm 
Offline

Joined: Mon Nov 29, 2010 7:18 pm
Posts: 7
It's not in the adobe documentation, but when you are extending you have to declare inspectables slightly differently.

ie. [Inspectable(name="orientToFocus", variable="orientToFocus", type="Boolean", defaultValue="false")] and they have to go before the class definition. Mine weren't showing up until i declared in this way. maybe thats the issue?

as for the camera class, well there is nothing in it at the moment except for an onEnterframe routine that does some automated camera panning/zooming, you could pass in any sprite or entity as the focusOn argument and it if orientToFocus was enabled it would use that as a 'camera' just like you were planing

here is my camera
Code:
   public class Camera extends Entity
   {
      
      public function Camera(){
         this.addEventListener(Event.ENTER_FRAME,enterFrame);
         
      }
      private var n:Number=0;
      private function enterFrame(e:Event):void
      {
         //move the cam and make it loop back to origin
         this.y          = 225 + Math.sin(n)*225;
         this.scaleX    = 3 + Math.sin(n)*3;
         this.scaleY    = 3 + Math.sin(n)*3;
         this.rotation    = (n / 8)*30;
         n+=0.05;
      }
   }


here is my subclass of World

Code:
   [Inspectable(name="orientToFocus", variable="orientToFocus", type="Boolean", defaultValue="false")]

      
    public class DWorld extends World {
      
   public var orientToFocus:Boolean;



...

   
      public override function scrollRotation():Number {
         
         //if orient to gravity set, do rotations of the world based on gravity
         if(orientToGravity) {
            var b:BodyShape = focus as BodyShape;
            var g:V2 = getGravityFor(V2.fromP(pos).divideN(scale), b ? b.b2body : null, b);
            return (Math.atan2(g.y, -g.x) * Util.R2D) - 90;
         }
         
         //if orient to focus set, manipulate the world based on the properties of the focus object (ie. the camera)
         if(orientToFocus) {
            scaleX = focus.scaleX;
             scaleY = focus.scaleY;
             return focus.rotation;
         }
         
         //else just return standard rotation
         return rot;
         
      }

    }
   


Top
 Profile  
 
PostPosted: Wed Dec 08, 2010 3:44 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
I really wasn't even sure if I should include scrolling stuff in the core anyway, never been completely happy with it but it works for my purposes. So don't consider it golden code never meant to be hacked at. I also don't want to clutter core with a ridiculous scrolling engine when most physics games I've seen don't scroll at all anyway.

The box man demo shows a camera that follows the player. You could also animate the camera and create an auto-scrolling world by setting the focus to any other display object (like sol is doing). Can even use tweens to animate the camera. Although in this case it would probably be simpler just to tween the whole world and use no camera...

Also there's the "startFocusTween" function if you suddenly want to change the camera to something else and do some easing.

solconnection: Extending components and adding inspectables works no differently. wck.World (a component) itself extends misc.Scroller (also a component).


Top
 Profile  
 
PostPosted: Thu Dec 09, 2010 1:19 am 
Offline

Joined: Mon Nov 29, 2010 7:18 pm
Posts: 7
mayobutter wrote:
solconnection: Extending components and adding inspectables works no differently. wck.World (a component) itself extends misc.Scroller (also a component).


fwiw, i read it here: http://forums.adobe.com/thread/453659 *and* my inspectables didn't show up on top of yours until i declared them before the class. It's odd.


Top
 Profile  
 
PostPosted: Thu Dec 09, 2010 11:04 am 
Offline

Joined: Mon Apr 07, 2008 3:06 am
Posts: 99
@solconnection...
Well I'll be dammed! :shock: After all the exploration I've done into building components, I can't believe there are still more quirks to be learned. I wrote a tutorial on the subject of building components with your own control panel and live preview, which is so quirky that most people would think it just didn't work at all without just the right voodoo dust.

Thank you very much for bringing that to light!

@mayobutter...
Quote:
I really wasn't even sure if I should include scrolling stuff in the core anyway, never been completely happy with it but it works for my purposes. So don't consider it golden code never meant to be hacked at. I also don't want to clutter core with a ridiculous scrolling engine...

I think you've taken just the right approach: simple, basic scrolling functionality built in, which can always be developed upon for specific projects. I would suggest though that the easing option as I've written ought to be included. It barely adds about five lines of code, and with the additional parameter you can turn it off if you want.

I like that idea of optionally rotating to orient to gravity, which probably ought to have its own easing parameter as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group