cannon

hi,

i'm trying to do a cannon which shoots circles.
so i will use addChild to attach the objects to the field.
But i cannot seem to find a way to applyForce or applyImpulse for the added child.
i'm also using myWorld1x.triangle.rotation = int(radAngle*360/(Math.PI*2)-90); so the applyForce or applyImpulse should contain the current angle. How to achieve this?

Thanks!
Quote:
myWorld1x.triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
so the applyForce or applyImpulse should contain the current angle. How to achieve this?


Well you can convert radians in to vectors like this:

function radianToVector(radian:Number):V2
{
return V2(Math.cos(radian), Math.sin(radian));
}

Then just shoot it by the returned value.

So you can find the X of a vector by using:
Math.cos(radian);
And the Y by
Math.sin(radian);

</mathLesson>
But how do i shoot it? How do apply force?
myObject.b2body.appyForce()
I think, or applyImpluse()

Or you can just change the x/y velos.
Code:
   var xd:Number;
   var yd:Number;
   var radAngle:Number;


   function mcFunction(event:Event):void {
      xd=myWorld1x.triangle.x-myWorld1x.mouseX;
      yd=myWorld1x.triangle.y-myWorld1x.mouseY;
      radAngle=Math.atan2(yd,xd);
      myWorld1x.triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
      if (myWorld1x.triangle.rotation>90) {
         myWorld1x.triangle.rotation=90;
      } else if (myWorld1x.triangle.rotation<0) {
         myWorld1x.triangle.rotation=0;
      }
   }
   stage.addEventListener(Event.ENTER_FRAME, mcFunction);

   function shootShot(event:MouseEvent):void {
      var shot:vinyl = new vinyl();
      shot.x=myWorld1x.triangle.x;
      shot.y=myWorld1x.triangle.y;
      myWorld1x.addChild(shot);
      myWorld1x.shot.b2body.ApplyImpulse(new V2(Math.cos(radAngle)*2, Math.sin(radAngle)*2),myWorld1x.shot.b2body.GetWorldCenter());
   }
   stage.addEventListener(MouseEvent.MOUSE_DOWN, shootShot);


the problem is this line:
Code:
myWorld1x.shot.b2body.ApplyImpulse(new V2(Math.cos(radAngle)*2, Math.sin(radAngle)*2),myWorld1x.shot.b2body.GetWorldCenter());


i'm getting now a error:

TypeError: Error #1010: A term is undefined and has no properties.
at <anonymous>()

i tried also to remove myWorld1x in front of the shot but that didnt work also
trace(myWorld1x.shot.b2body)

See if it exists.
i cannot trace it as the error stops the script. A term is undefined and has no properties.
master wrote:
i cannot trace it as the error stops the script. A term is undefined and has no properties.


Well put it before the errored script. 😛
i cannot trace it as it is not defined
master wrote:
i cannot trace it as it is not defined

-_-
Code:
function shootShot(event:MouseEvent):void {
      var shot:vinyl = new vinyl();
      shot.x=myWorld1x.triangle.x;
      shot.y=myWorld1x.triangle.y;
      myWorld1x.addChild(shot);
trace(myWorld1x.shot.b2body);
      myWorld1x.shot.b2body.ApplyImpulse(new V2(Math.cos(radAngle)*2, Math.sin(radAngle)*2),myWorld1x.shot.b2body.GetWorldCenter());
   }


I think the problem is that WCK tends not to make the MCs in to physical objects for a while. :/
Like maybe during the next step. So if b2body doesn't exist then just set it's velos, so that when it is created it starts flying at the angle.

So set:
shot.linearVelocityX = Math.cos(radAngle)*2
shot.linearVelocityY = Math.sin(radAngle)*2

Also I don't know where you got the *2 thing from.

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