Local Velocity

  ... tutorials on how to use AndEngine.

Local Velocity

Postby NightJumper » Tue Oct 05, 2010 9:03 pm

So was messing around with applying velocity and acceleration to sprites based on a local coordinate system of the sprite, this is useful at least for my game when using the accelerometer I want sprite to always move forward in relation to its direction when the phone is tilted forward. So instead of always having to calculate the required X and Y components to find what I need every time I want to adjust speed I made a couple of methods to do it and that way I just apply it to the local coordinate system of the sprite.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void setLocalVelocity(float X, float Y){
  2.         final float playy=((float) Math.cos(MathUtils.degToRad(this.getRotation()))*Y)+(float) Math.sin(MathUtils.degToRad(this.getRotation()))*X;
  3.         final float playx=((float) Math.sin(MathUtils.degToRad(this.getRotation()))*-Y)+((float) Math.cos(MathUtils.degToRad(this.getRotation()))*X);
  4.         this.setVelocity(playx, playy);
  5.     }
  6.     public void setLocalAcceleration(float X, float Y){
  7.         final float playy=((float) Math.cos(MathUtils.degToRad(this.getRotation()))*Y)+(float) Math.sin(MathUtils.degToRad(this.getRotation()))*X;
  8.         final float playx=((float) Math.sin(MathUtils.degToRad(this.getRotation()))*-Y)+((float) Math.cos(MathUtils.degToRad(this.getRotation()))*X);
  9.         this.setAcceleration(playx, playy);
  10.     }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


So using setLocalVelocity(0,10) would always move the sprite in the positive Y direction with respect to it (which in most cases would be down

and setting setLocalVelocity(10,0) would always move the sprite in the positive X direction with respect to itself (should be to the right)
NightJumper
 
Posts: 98
Joined: Wed Sep 01, 2010 9:42 pm

Re: Local Velocity

Postby Nicolas Gramlich » Tue Oct 05, 2010 11:18 pm

Moved to Tutorials subforum.
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Local Velocity

Postby NightJumper » Wed Oct 06, 2010 12:36 am

Ok thats cool, was kinda thinking it could be a quick addition to moveable objects to add in the source, a nice feature I can see a lot of uses for, but this works also.
NightJumper
 
Posts: 98
Joined: Wed Sep 01, 2010 9:42 pm


Return to Tutorials

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher, RonScript and 21 guests