Corona SDK dynamically change sprite speed - animation

I am trying to make a sprite dance according to the beat of a song in Corona SDK. So far I have managed to get periodic samples of frequency for the song playing but I can't seem to dynamically change the sprite movement speed so it matches the frequency.Do you have any idea how can I make this happen?

Just create multiple sequences each with its own play time and switch them using 'prepare'.
Like that:
sprite.add( spriteSet, "slow", 1, 10, 1000, 0 )
sprite.add( spriteSet, "fast", 1, 10, 500, 0 )
local instance = sprite.newSprite( spriteSet )
instance:prepare("slow")
instance:play()
When music gets faster:
instance:prepare("fast")
instance:play()

Related

Vertical takeoff for a DJI Matrice 100

DJI Android SDK version: 4.11
Matrice 100 / Matrice 600
I am trying to take off the drone vertically.
I tried with GoToAction in a timeline, but that failed due to some bug in the SDK, (confirmed by your support team dev#dji.com #29496) I get STARTED for the GoToAction, but no PROGRESSED or FINISHED, and no errors logged at all.
Since I need to continue working, I tried a workaround by sending FlightControlData to the VirtualStick by calling the following function with the requested height 20 times a second:
VerticalControlMode.POSITION
FlightOrientationMode.AIRCRAFT_HEADING
VirtualStickModeEnabled = true
VirtualStickAdvancedModeEnabled = true
void sendHeightCommand(Float requestedAltitude) {
FlightControlData data = new FlightControlData(0f, 0f, 0f, requestedAltitude);
flightController.sendVirtualStickFlightControlData(data, djiError -> {
log.v(djiError.getDescription);
});
}
And it works (with the right amount of timeouts) but if there is wind, the drone drifts away, which is very dangerous for me as there is more than one drone in the field, and I don't want them to collide.
Is there another way to change the altitude of the drone, while maintaining its position?
Or is there a way to measure the wind, and push back against it?
[*] Take off drone vertically:
I always use the TakeOffAction in the timelineMission before the GoToAction to ascend to the desired height. However I'm using a Mavic Pro and the SDK may behave different with a matrice drone.
When using the FlightControlData with the VirtualSticks, I use the startPrecisionTakeoff() method in the FlightController class; after the takeoff, the drone ascends to the desired position when the flight control data is sent continuously.
[*] stable hovering:
For the hovering the only low cost solution I see is to enable the VisionAssistedPositioning in the FlightAssistant class, I don't know if the Matrice supports this feature as the documentation doesn't say anything on the supported aircrafts.
Ok so the solution was to use the function: setVirtualStickAdvancedModeEnabled(true)
The reason I didn't see any results was because in the simulator I was playing with 20.0 North Wind, which apparently is too much.
When I lowered it to 5.0 it works perfectly.

Creating a ring/circle sector with the CanvasRenderer

Trying to create a number of circle/ring segments using Three.JS and the CanvasRenderer.
Example using THREE.Shape and THREE.ShapeGeometry here: http://jsfiddle.net/25U8E/1/
The triangulation looks wrong to me and when I tried to move the vertices by directly modifying geometry.vertices[n].x etc. the THREE.Shape I am using degrades and becomes less smooth.
Is there a better way to create a sector - one that allows me to modify the vertices afterwards?
Not sure if this is the Stackoverflow-y thing to do but here goes:
The right way to create a ring sector like I need is to use the RingGeometry class like this:
THREE.RingGeometry( 150, 300, 16, 2, 0, Math.PI / 4 );
It does contain a bug that means some of the verts are wrong - illustrated here: http://jsfiddle.net/NWJQk/ - as it spins, you'll see the back side is wrong.
That fiddle references the URL for the latest version of Three.JS so if it starts working, it means the bug was fixed and released.
Edit: Clearer illustration of what's failing using wireframe: http://jsfiddle.net/NWJQk/3/

Limiting WebGL Framerate via setInterval severely decreases performance

I am creating a webgl game, I have it fairly well optimized, however there is one problem, my framerate limiter destorys performance. I know what your thinking "duh, of course it is...its an fps limiter". Well the issue is that it is not behaving at expected. Here is the code:
renderTimer = null;
function animate() {
clearTimeout(renderTimer);
renderTimer = setTimeout(function () {
_frame = requestAnimationFrame(animate);
}, 33);
render();
}
function render(){
// operations for mesh positioning/animation
handleObjects();
renderer.render(scene, camera);
}
On my desktop this works as expected, gameplay is smooth and is held at 29-30fps.
On my laptop fps drops to 22-24 and gameplay is jerky. If I change the interval delay to 16ms, gameplay is relatively smooth and holds at about 35fps. If I remove the interval all together gameplay is completely smooth and holds at about 45fps.
I dont completely understand this behavior. If the cap is 30fps why does my laptop performance drop below 25fps? I would expect it to be also 25fps without the interval, but yet it goes faster. Curious.
I would happily just remove the interval however I do want my fps capped at 30, players getting a higher fps than that would be at an advantage.
Thoughts?
There are a couple of considerations:
setTimeout in javascript can not really be counted on being very accurate.
Browser can do it's own things like garbage collection at any time, delaying stuff.
Rendering itself (webgl/three.js + your own game logic) takes time. Even though you create the timeout before main render call, it still introduces 33msec idle time.
Well, I'm not actually sure about the last point, now that I think about it. Anyway I have observed similar problems, and I managed to make a solution that works pretty OK, it will keep the framerate smooth and +/- 1-2 FPS accurate to a target framerate if the computer can handle such framerate at all. It's a hack though.
First you could take a look at the requestAnimationFrame implementation in Three.js (for browsers that do not have it built-in):
requestAnimationFrame = function ( callback ) {
var currTime = Date.now(), timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
var id = self.setTimeout( function() { callback( currTime + timeToCall ); }, timeToCall );
lastTime = currTime + timeToCall;
return id;
};
You can see it adjusts the setTimeout based on the last call (targeting around 60FPS). So that's one solution you could try.
What I did (the hack that seems to work well), is to have the timeout value as a variable, with initial value being the original 33 or whatever the desired framerate is. Then on each frame, I record the Date.now(), and compare it to the previous frame time. If we missed the budgeted frame time, we decrement the timeout value by 1. If we were faster than desired, we increment timeout by 1. So the code is continually, smoothly, adjusting the timeout to match desired framerate. By only slightly incrementing/decrementing, we avoid the problems with unpredictable garbage collections etc completely throwing off calculations and messing things up. And it just works (tm)
I won't post the code because there is so much more going on my render loop (only render new frame if something is changed etc), it would be tedious to isolate a relevant code example.

How can I correct TouchPanel offset bug with Windows Phone XNA game on WP8 720p devices?

XNA apps (WP7 or WP7.5 apps) that run on a WP8 720p device will get automatically letterboxed so the 480x800 BackBuffer size stays the same (for compatibility I presume).
This would be fine, except there appears to be a bug in the XNA compatibility layer. The TouchPanel reports touch locations that are off by the size of the top letterbox blank area.
This has two problems:
The user's touches will appear to be off making gameplay and menu navigation difficult.
Because it is off in the negative direction, the user will be unable to touch things at the very bottom of the screen at all.
I tried working around the issue by just factoring in 53 / 2 pixel offset (53 is the total amount of extra space in scaled coordinate, divide by two because it is only off by one letterbox bar - the one on the top). This does correct the touch location, but because TouchPanel internally clamps negative values to 0, it means that there is still a dead zone at the top of the game (because -22 through -1 should be translated to 0 through 22, but if all negative input values are clamped to 0 then information is lost and everything in the negative range will translate to 22 always).
Has anyone run into this and found a way to work around it?
I'v even tried resetting the TouchPanel.DisplayHeight/Width to the actual 720p values of the device and somehow it gets reset to 480x800 by the next frame update.
I just got this working, the TouchPanel.DisplayHeight needs to be set to 853 (if you detect you are on one of these 720p devices) very early. I do it at OnNavigatedTo from the main Silverlight page (this is SL/XNA actually).
Next, you have to offset every touch location and gesture location by + 53.0f / 2.0f.
I'm not sure why this didn't work before, as this is the solution I mentioned above that didn't seem to work because TouchPanel kept resetting to 800 height.
But, I got it working in both a reduced repro (new SL/XNA app) and my main game app.
I was working on a game a couple days ago.
It was packaged for 7.1, but worked fine on the 720p emulator.
I don't know much about the compatibility layer, if it gets effected by the size of images then here goes :
I created separate images for WVGA, WXGA and 720p. Used them and found out about the letterboxing and decided to use 720p images for all.
Probably doesn't help but there you go anyway.
This is great solutio what I found from here: http://developer.nokia.com
It's not just fixing issue with touch but it also remove black blocks from side. Of course depending about your programn this can cause some more issues since resolution and screen ratio will change.
if (Environment.OSVersion.Version.Major == 8)
{
int? scaleFactor = null;
var content = System.Windows.Application.Current.Host.Content;
var scaleFactorProperty = content.GetType().GetProperty("ScaleFactor");
if (scaleFactorProperty != null)
{
scaleFactor = scaleFactorProperty.GetValue(content, null) as int?;
}
if (scaleFactor == null)
scaleFactor = 100;
if (scaleFactor == 150)
{
SharedGraphicsDeviceManager sdm = SharedGraphicsDeviceManager.Current;
sdm.PreferredBackBufferHeight = 800;
sdm.PreferredBackBufferWidth = 450;
TouchPanel.DisplayHeight = 800;
TouchPanel.DisplayWidth = 450;
}
}

Slowing down looping animation as3

This is the first time I've ever posted in a forum, so thanks in advance for anyone who takes the time to read/answer this question.
What I'm trying to create is basically a flipping coin animation, which starts off turning very fast and then slows down to stop with a (randomly generated) side facing upwards after about 8 seconds.
I've done the animation of a complete flip, which lasts about half a second, and made it in to a movieclip... now I'm stuck!
Any ideas how I might go about doing this in actionscript3?
The fastest way around this would be to use some very basic actionscript. First, create 2 animations (One heads, one tails). Now, you only need a single frame for this and don't need to place the movieclips on the stage. Use the following or similar code:
var whichSide:int = 0;
var coin1:coinAnimation1 = new coinAnimation1();
var coin2:coinAnimation2 = new coinAnimation2();
whichSide = math.Round(math.Random(1));
if(whichSide == 1)
{
addChild(coin1);
}
else
{
addChild(coin2);
}
Just don't forget to right click the movieclip and export for actionscript, giving the movieclips the class of: coinAnimation1 and coinAnimation2.
Hope this helps.
I've accomplished such animation on ´Keyframes´ using the Tweener class. You can easily tween on the keyframe parameter with specific transition...
Basic example:
Tweener.addTween(myMovieClip, {_frame:10, time:2.5});
More information about Tweener here

Resources