I'm making a little game for iPhone that involves whistling. The phone hears the frequency of your whistle, and sets the position of a sprite on the screen accordingly. Here is the code:
if frequency > 1000 then
soundbar.y = (frequency-1020)/10+30
end
This makes the bar move between 30 and higher on the y axis, with the highest whistling register being at about y 400, which is fine.
There is only one problem. The higher you whistle, the further down the bar goes. Is there any way I can edit my formula, so that the bar starts at 208, and as the pitch increases, the bar rises?
Thanks :) This has had me stumped for a while!
208 - ((frequency-1020)/10+30)
which simplifies to...
178 - ((frequency-1020)/10)
or even...
280 - (frequency/10)
Related
My code for physics in my game is this:
-- dt = time after each update
self.x = math.floor(self.x + math.sin(self.angle)*self.speed*dt)
self.y = math.floor(self.y - math.cos(self.angle)*self.speed*dt)
-- addVector(speed,angle,speed2,angle2)
self.speed,self.angle = addVector(self.speed,self.angle,g,math.pi)`
when it hits the ground, the code for it to bounce is :
self.angle = math.pi - self.angle
self.y = other.y - self.r`
the function addVector is defined here:
x = math.sin(angle)*speed + math.sin(angle2)*speed2
y = math.cos(angle)*speed + math.cos(angle2)*speed2
v = math.sqrt(x^2 + y^2)
a = math.pi/2 - math.atan(y,x)
return v,a
but when I place a single ball in the simulation without any drag or elasticity, the height of the ball after each bounce keeps getting higher. Any idea what may be causing the problem?
Edit: self.r is the radius, self.x and self.y are the position of the centre of the ball.
Your Y axis is increasing downward and decreasing upward.
Making self.y = math.floor(..) moves your ball a bit upward every frame.
The solution is to store your coordinates with maximal precision.
You could make new variable y_for_drawing = math.floor(y) to draw the ball at pixel with integer coordinates, but your main y value must have fractional part.
I’ve managed to get your code to run and reproduce the behavior you are seeing. I also find it difficult to figure out the issue. Here’s why: movement physics involves position, which is affected by a velocity vector, which in turn is affected by an acceleration vector. In your code these are all there, but are in no way clearly separated. There are trig functions and floor functions all interacting in a way that makes it difficult to see what role they are playing in the final position calculation.
By far the best and easiest-to-understand tutorial to help you implement basic physics lime this is The Nature of Code (free to read online, with interactive examples). As an exercise I ported most of the early exercises into Lua. I would suggest you see how he clearly separates the position, velocity and acceleration calculations.
As an experiemnt, increase g to a much higher number. When I did that, I noticed the ball would eventually settle to the ground, but of course the bouces were too fast and it didnt bounce in a way that seems natural.
Also, define other.y - it doesnt seem to affect the bouncing issue, but just to be clear on what that is.
I am having issues with my implementation of CorePlot. I have a subview in my UIViewController than contains the chart. Most of the data shows up fine but no increments / values are showing up on the y axis.
This is what I am seeing now:
If I shift the x axis start point by subtracting 10 (to better see the left half of the chart) I see this:
For the record, I do not have any values outside of the (positive x, positive y) quadrant on the chart and have logged out my data to verify this. Any input / advice would be appreciated, thank you!
http://www.raywenderlich.com/13271/how-to-draw-graphs-with-core-plot-part-2
This guy does a great job explaining that about halfway down... search for "You're Getting There!" to find exactly where he talks about the weird lines in the Y axis.
Hope this helps!
Set the leftPadding on the graph.plotAreaFrame to leave room for the axis labels at the left side of the graph.
The smeared y-axis is caused by too many tick marks and labels—they overlap and become illegible and negatively impact drawing performance. You need to adjust the labeling parameters to reduce the number of ticks and labels. By default, the axis draws major tick marks and labels one unit apart. The properties that need to be changed depend on the labeling policy you want to use.
I am using the device accelerometer and try to smooth the Accelerometer Data CMAcceleration.
I am doing this with help of this code:
-(void)proccessAccelerometerData:(CMAcceleration)accelData {
currentAccelX = (kUpdateInterval * accelData.x) + ((1.0 - kUpdateInterval) * currentAccelX);
currentAccelY = (kUpdateInterval * accelData.y) + ((1.0 - kUpdateInterval) * currentAccelY);
}
Which currentAccelX and currentAccelY is the last accelerometer x and y data.
Now, I have the smooth x and y values, What is my value of x or y to determine id the user move the device left/right/up/down?
Just to make things more clear, for example, let's say that I have 4 buttons, one for each direction left/right/up/down and I want to determine which direction the user user swipe the device? (not swipe gesture).
Thanks in advance!
Assuming you hold the device in portrait orientation, the x-Axis indicates the movement to the left and right (positive x is to the right and negative x to the left).
The y-Axis indicates the movement of the device up and down (positive y is upwards and negative y is downwards).
The z-Axis indicates the movement of the device forwards and backwards (positive z is towards the user and negative z is away from the user).
There is an info graphic by Apple: developer.apple.com
I'm trying to apply an ease to a panning gesture, so the further I pan my finger, the less my effective panning distance is - so I can make it feel like the object I am panning is on a spring.
I guess I need to apply some sort non-linear of curve to the distance I am panning. Pseudo-idea below:
RAW POINTS PANNED
1 - 25 - 50 - 100
'CURVED' POINTS PANNED
1 - 20 - 35 - 45
I'll then use the 'curved' pixels panned to move my UIViews.
I appreciate this may come across as bit vague, I'm not really sure how best to describe it. Any help I can get would be greatly appreciated, I can try to describe my problem better if anyone needs.
You could use a function like x^n, 0 < n < 1
Raw points panned = x
Curved points panned = x^n
Then you could experiment with different values of n. eg. x^0.8 would give roughly:
Raw points panned = 1 - 25 - 50 - 75 - 100
Curved points panned = 1 - 13 - 23 - 32 - 40
I'm not very familiar with objective-c, but if you have the variable rawPointsPanned, you could convert that to curved points panned with something like this (using java code sorry):
curvedPointsPanned = (int) pow(rawPointsPanned, 0.8);
I want to add force to the grenade according to the touch positions of the user.
--this is the code
physics.addBody(grenade1,"dynamic",{density=1,friction=.9,bounce=0})
grenade1:applyForce(event.x,event.y,grenade1.x,grenade1.y)
Here more the x and y positions are the lower the force is. But the force here is too high that the grenade is up in the sky.
You must calibrate the force applied. Remember that F=ma so if x=250 then F=250, if the mass of the display object (set when added body, based on material density * object area) is 1 then acceleration a = 250, which is very large. So try:
local coef = 0.001
grenade1:applyForce(coef*event.x, coef*event.y, grenade1.x, grenade1.y)
and see what you get. If too small, increase coef until the response is what you are looking for. You may find that linear (i.e., constant coef) doesn't give you the effect you want, for example coef=0.01 might be fine for small y but for large y you might find that coef=0.001 works better. In this case you would have to make coef a function of event.y, for example
local coef = event.y < 100 and 0.001 or 0.01
You could also increase the mass of the display object, instead of using coeff.
Recall also that top-level corner is 0,0: force in top level corner will be 0,0. So if you want force to increase as you go up on the screen, you need to use display.contentHeight - event.x.