libgdx/box2d lights: change blur of lights - libgdx

I was wondering whether it is possible to change the rate at which a lights intensity decareases over distance.
something like this:

So I finally figuered it out.
You have to write a custom shader that is essetially the same as the default one, but change the line that takes care of the interpolation:
"v_color = s*quad_colors;\n"
for example:
"v_color = s*2*quad_colors;\n"
halves the dropoff rate, while:
"v_color = (s*0)+quad_colors;\n"
gets rid of any blur (leaving out the "s" completely out won't work)

I have the "v_color = squad_colors;\n" its in the vertex shader of the light source. See https://github.com/libgdx/box2dlights/blob/master/src/shaders/LightShader.java. However the above didn't work for me, the number you use must be a float. E.g."v_color = (s0.0)+quad_colors;\n"

Related

Keeping the current CCSprite to another scenes

I'm a beginner of using cocos2d-x.
My problem is I dun know how to keep the CCSprite to another scenes.
The details of my case:
I've made a class"Scene01"includes 5 characters CCSprite with attributes, each of them class name like C1,C2...C5.
I've made a "Draw" button at class"Scene02"to draw out 1 of them randomly. I put this action at "CCTouchesBegan"...the character draw setting as below:
if (probability >0 && probability <=20) {result = C1::create();}
else if (probability >20 && probability <=40){result = C2::create();}
...until C5::create();
I use "this->addChild(result);" display on "Scene02" at "CCTouchesBegan".
But I don't know how to keep the generated "result(CCSprite)" to the new scene class"Scene03". Is there any better way(s) to simplify my case or any method(s) can help me to complete it?
You could try the following: retain the Sprite, remove it from Scene02 (keeping it on the heap) and then add it to the Scene03.
//(Scene02)
result->retain();
result->removeFromParent();
..
//(Scene03)
this->addChild(result);
result->release();

Inverted Smoothstep?

I am currently trying to simulate ballistics on an object, that is otherwise not affected by physics. To be precise, I have a rocket-like projectile, that is following an parabolic arc from origin to target with a Lerp. To make it more realistic, I want it not to move at constant speed, but to slow down towards the climax and speed up on its way back down.
I have used the Mathf.Smoothstep function to do the exact opposite of what i need on other objects, i.e. easing in and out of the motion.
So my question is: How do I get an inverted Smoothstep?
I found out that what i would need is actually the inverted formula to smoothstep [ x * x*(3 - 2*x) ], but being not exactly a math genius, I have no idea how to do that. All I got from online calculators was some pretty massive new function, which I'm afraid would not be very efficient.
So maybe there is a function that comes close to an inverted smoothstep, but isn't as complex to compute.
Any help on this would be much appreciated
Thanks in advance,
Tux
Correct formula is available here:
https://www.shadertoy.com/view/MsSBRh
Solution by Inigo Quilez and TinyTexel
Flt SmoothCubeInv(Flt y)
{
if(y<=0)return 0;
if(y>=1)return 1;
return 0.5f-Sin(asinf(1-2*y)/3);
}
I had a similar problem. For me, mirroring the curve in y = x worked:
So an implementation example would be:
float Smooth(float x) {
return x + (x - (x * x * (3.0f - 2.0f * x)));
}
This function has no clamping, so that may have to be added if x can go outside the 0 to 1 interval.
Wolfram Alpha example
If you're moving transforms, it is often a good idea to user iTween or similar animation libraries instead of controlling animation yourself. They have a an easy API and you can set up easing mode too.
But if you need this as a math function, you can use something like this:
y = 0.5 + (x > 0.5 ? 1 : -1) * Mathf.Pow(Mathf.Abs(2x - 1),p)/2
Where p is the measure of steepness that you want. Here's how it looks:
You seem to want a regular parabola. See the graph of this function:
http://www.wolframalpha.com/input/?i=-%28x%2A2-1%29%5E2%2B1
Which is the graph that seems to do what you want: -(x*2-1)^2+1
It goes from y=0 to y=1 and then back again between x=0 and x=1, staying a bit at the top around x=0.5 . It's what you want, if I understood it correctly.
Other ways to write this function, according to wolfram alpha, would be -(4*(x-1)*x) and (4-4*x)*x
Hope it helps.

How to get the real height of a character (fontmetrics/graphics2d)

I am currently in need to get the real height of a character. I am aware of the functions like getDecsent(), getAscent(), ... but they only allow to get values regarding the hole font (in its context). I also tried the way using getStringBounds(), but that is the same story.
Like the title says, I am looking for a way to get the height value of just one char at a time.
For example 'N' is higher then 'n', 'I' just a little bit higher then 'i' and so on
Thanks for your time
Use this
Rectangle2D bounds = font.getStringBounds("put your string here", context);
//font can be set to whatever you want
//my suggestion is that you use the same font for both characters
//context is a object of FontRenderContext
System.out.println(bounds.getHeight());
//Instead of just printing it you could do this a second time and compare the 2 strings

more minimaler cubism.js horizon chart from json example

Following up on a previous question... I've got my minimal horizon chart example much more minimaler than before ( minimal cubism.js horizon chart example (TypeError: callback is not a function) )
<body>
<div class="mag"></div>
<script type="text/javascript">
var myContext = cubism.context();
var myMetr = myContext.metric(function(start, stop, step, callback) {
d3.json("../json/600s.json.php?t0=" + start/1000 + "&t1=" + stop/1000 + "&ss=" + step/1000, function(er, dt) {
if (!dt) return callback(new Error("unable to load data, or has NaNs"));
callback(null, dt.val);
});
});
var myHoriz = myContext.horizon()
.metric(myMetr);
d3.select(".mag")
.call(myHoriz);
</script>
</body>
The d3.json() bit calls a server side .php that I've written that returns a .json version of my measurements. The .php takes the start, stop, step (which cubism's context.metric() uses) as the t0, t1, and ss items in its http query string and sends back a .json file. The divides by 1000 are because I made my .php expect parameters in s, not ms. And the dt.val is because the actual array of my measurements is in the "val" member of the json output, e.g.
{
"other":"unused members...",
"n":5,
"val":[
22292.078125,
22292.03515625,
22292.005859375,
22292.02734375,
22292.021484375
]
}
The problem is, now that I've got it pared down to (I think) the bare minimum, AND I actually understand all of it instead of just pasting from other examples and hoping for the best (in which scenario, most things I try to change just break things instead of improving them), I need to start adding parameters and functions back to make it visually more useful.
Two problems first of all are, this measurement hovers all day around 22,300, and only varies +/- 10 maybe all day, so the graph is just a solid green rectangle, AND the label just says constantly "22k".
I've fixed the label with .format(d3.format(".3f")) (versus the default .2s which uses SI metric prefixes, thus the "22k" above).
What I can't figure out is how to use either axis, scale, extent, or what, so that this only shows a range of numbers that are relevant to the viewer. I don't actually care about the positive-green and negative-blue and darkening colours aspects of the horizon chart. I just used it as proof-of-concept to get the constantly-shifting window of measurements from my .json data source, but the part I really need to keep is the serverDelay, step, size, and such features of cubism.js that intelligently grab the initial window of data, and incrementally grab more via the .json requests.
So how do I keep the cubism bits I need, but usefully change my all-22300s graph to show the important +/- 10 units?
update re Scott Cameron's suggestion of horizon.extent([22315, 22320])... yes I had tried that and it had zero effect. Other things I've changed so far from "minimal" above...
var myHoriz = myContext.horizon()
.metric(myMetr)
.format(d3.format(".2f"))
.height(100)
.title("base1 (m): ")
.colors(["#08519c", "#006d2c"])
// .extent([22315, 22320]) // no effect with or without this line
;
I was able to improve the graph by using metric.subtract inserting it above the myHoriz line like so: (but it made the numerical label useless now):
var myMetr2 = myMetr.subtract(22315);
var myHoriz = myContext.horizon()
.metric(myMetr2)
.format...(continue as above)
All the examples seem so concise and expressive and work fine verbatim but so many of the tweaks I try to make to them seem to backfire, I'm not sure why that is. And similarly when I refer to the API wiki... maybe 4 out of 5 things I use from the API work immediately, but then I always seem to hit one that seems to have no effect, or breaks the chart completely. I'm not sure I've wrapped my head around how so many of the parameters being passed around are actually functions, for one thing.
Next hurdles after this scale/extent question, will be getting the horizontal time axis back (after having chopped it out to make things more minimal and easier to understand), and switching this from an area-looking graph to more of a line graph.
Anyway, all direction and suggestion appreciated.
Here's the one with the better vertical scale, but now the numerical label isn't what I want:
Have you tried horizon.extent? It lets you specify the [min, max] value for the horizon chart. By default, a linear scale will be created to map values within the extent to the pixels within the chart's height (specified with `horizon.height or default to 30 pixels).

Getting an unexpected trace for getpixel32. Can anybody see why?

When running the following code, "-2" is being traced and I am wrecking my head trying to understand why.
var bmd:BitmapData = new BitmapData(1,1,true,0xFFFFFFFF);
bmd.setPixel32(0,0, 0x32FF6B45);
trace(0x32FF6B45-bmd.getPixel32(0,0));
As far as I can tell, it should trace 0. 0x32FF6B45 is initially assigned to the pixel at coords 0,0. That value should be returned in bmd.getPixel32(0,0) and then, when it's subtracted from 0x32FF6B45, it should result in 0. Why the heck am I getting -2?
EDIT:
I've traced out the values individually and it makes sense that the operation in the trace above results in -2 because tracing out 0x32FF6B45 results in 855599941 and tracing out bmd.getPixel32(0,0) results in 855599943. The question now is why the heck are those values different? Whey doesn't bmd.getPixel32(0,0) also trace out 855599941?
I have the same problem, and I believe it is related to premultiplied alpha, as described here. In my code I was setting a pixel to 0xa08800ff and getting back 0xa08700ff. If you need alphas other than 0xff, then unfortunately it may be necessary to simultaneously store all your pixel values in a separate data structure too.
That is expected.
getPixel
This will return a value: #RRGGBB (rgb / red, green, blue)
getPixel32
This will return a value: #AARRGGBB (argb / alpha, red, green, blue)
Example:
trace('test 0x32FF6B45: '+0x32FF6B45);
var bmd:BitmapData = new BitmapData(1,1,true,0xFFFFFFFF);
trace('setting 0,0 to 0x32FF6B45');
bmd.setPixel32(0,0, 0x32FF6B45);
var color:* = bmd.getPixel32(0,0)
trace('0,0: '+color);
trace(color-bmd.getPixel32(0,0));
Results:
test 0x32FF6B45: 855599941
setting 0,0 to 0x32FF6B45
0,0: 855599943
0
From what I can tell, you're using a color that is out-of-bounds to Flash. I'm not sure of the color range, but I know in previous experiences when taking photoshop elements with many colors, sometimes objects failed to import because the color value was out of bounds.
#Jari is also correct about the transparency.