Gravity issue using Farseer Physics - xna

Having an issue with farseer physics im pretty sure the issue is just with this line of code because of the y value. So when i try to move my character when falling he moves left and right normally but his fall slows.
body.LinearVelocity = new Vector2(1,0)
Is there a way to only change the x value of this? Or is there a way to prevent sliding and set a cap on the speed of applyforce() or applylinearimpulse()?

By setting the linear velocity to 1,0 you give the character a horizontal speed of 1 and a vertical speed of 0. So you effectively stop it from falling.
The following code will do what you would expect as it preserves the vertical speed.
body.LinearVelocity = new Vector2(1, body.LinearVelocity.y);
In some (most) cases it is better to apply a force or impulse to the character using body.Apply... this applies a force for a single frame and Farseer will automatically compute the correct velocity. Note that adding the same force or impulse every frame will make the movement speed-up.

Related

Gravity not working correctly

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.

SpriteKit: Node moves away when placed in the centre of spring field

Say we have scene with spring field node at the (0;0) and some node affected by this field.
Weird thing: if we put node at (0;0), it flies away immediately with huge speed; if put at some offset, like (0;10), it moves toward field node, oscillates for some time before stay at (0;0) and then flies away. Is there any way to prevent this?
Turned out this only appears in iOS 8.
Problem lurks in calculation of force multiplier. If we set falloff = 0 or min radius = 0, such behaviour doesn't appear. So setting minRadius to 0 solved the problem. Thanks #0x141E for support.
Upd:
I was wrong - setting minRadius doesn't solve the problem. I didn't noticed that field coordinates displayed as (0;0) in Scene Editor in a fact were non-zero, delta was about 1*10^-4, while node was placed exactly at (0;0). Such quite small initial distance seems to help, but can't say what will happen if node eventually stay at the centre of the field.
For this time solution is to add initial distance and wait for end of iOS 8 support for app.

To set accelerometer for corona game

I am creating a game which uses accelerometer to move ball (football model game).
function acc(e)
physics.setGravity(e.xInstant*(screenW/4), -1*e.yInstant*(screenH/4))
end
But this code not giving a smooth flow of game. Can u guys help me for right option
You either need to "calibrate" or to change the algorithm. I would try calibrate first: replace your acc() with
function acc(e)
local calibX = 1
local calibY = 1
-- physics.setGravity(calibX * e.xInstant*(screenW/4), -calibY * e.yInstant*(screenH/4))
print(e.xInstant, e.yInstant, e.zInstant)
end
and do the tilting that you feel is "not smooth", looking at the values printed when you do that. This will tell you what calibX and Y should be, set them then uncomment the physics line, comment out the print line, try again, until you get it right. For example, if the x and y instant are around 10, you could try calibX = 0.1 or less.
If you can't find values for calibration coefficients that give you desired motion, you need to change your algorithm. For example, if you are trying to simulate the tilt of your device as though it was a table on which a marble rolls, and tilting the table should make marble move, then changing gravity is not the way to do it. You want to apply a horizontal force which is equal to g*sin(theta) where g is gravity and theta is the tilt angle of the device side-to-side. The formula is slightly more complex if you allow tilt along the other direction.

Make Physics node move horizontal without spinning

Hello in a game I'm making using lua in Marmalade Quick,I have run into a problem with the physics.
I have a normal downward y gravity and have some notes that is affected by that.
Now I want to add some objects that "fly" horizontally on the X axis but I can not get it to work.
so one of the notes looks like this:
sky2 = director:createSprite(dw, 40, "textures/tractor.png")
physics:addNode(sky2, {type="dynamic"})
sky2.physics:setGravityScale(0)
my first thought was to
just add the following to an update listener
if(gameplaying == true) then
sky2.x = sky2.x-2.5
unfortunately this does not work after the node has got added physics
then I was looking into using
sky2.physics:applyapplyLinearImpulse or sky2.physics:applyForce
I used it like this
sky2.physics:applyapplyLinearImpulse(-10, 0, -20, 40)
The problem here is that the node then correctly moves along the axis but it is spinnig around (torque effects)..
Is there away to stop this or what am I doing wrong,,
thanks..
Found out that the Marmalade Quick Documentation was wrong, and to not input both a px and a px value but just 0 so sky2.physics:applyapplyLinearImpulse(-10, 0) this will apply the impulse at the centre of mass and make it move straight.

Repeating 2d world

How to make a 2d world with fixed size, which would repeat itself when reached any side of the map?
When you reach a side of a map you see the opposite side of the map which merged togeather with this one. The idea is that if you didn't have a minimap you would not even notice the transition of map repeating itself.
I have a few ideas how to make it:
1) Keeping total of 3x3 world like these all the time which are exactly the same and updated the same way, just the players exists in only one of them.
2) Another way would be to seperate the map into smaller peaces and add them to required place when asked.
Either way it can be complicated to complete it. I remember that more thatn 10 years ago i played some game like that with soldiers following each other in a repeating wold shooting other AI soldiers.
Mostly waned to hear your thoughts about the idea and how it could be achieved. I'm coding in XNA(C#).
Another alternative is to generate noise using libnoise libraries. The beauty of this is that you can generate noise over a theoretical infinite amount of space.
Take a look at the following:
http://libnoise.sourceforge.net/tutorials/tutorial3.html#tile
There is also an XNA port of the above at: http://bigblackblock.com/tools/libnoisexna
If you end up using the XNA port, you can do something like this:
Perlin perlin = new Perlin();
perlin.Frequency = 0.5f; //height
perlin.Lacunarity = 2f; //frequency increase between octaves
perlin.OctaveCount = 5; //Number of passes
perlin.Persistence = 0.45f; //
perlin.Quality = QualityMode.High;
perlin.Seed = 8;
//Create our 2d map
Noise2D _map = new Noise2D(CHUNKSIZE_WIDTH, CHUNKSIZE_HEIGHT, perlin);
//Get a section
_map.GeneratePlanar(left, right, top, down);
GeneratePlanar is the function to call to get the sections in each direction that will connect seamlessly with the rest of your world.
If the game is tile based I think what you should do is:
Keep only one array for the game area.
Determine the visible area using modulo arithmetics over the size of the game area mod w and h where these are the width and height of the table.
E.g. if the table is 80x100 (0,0) top left coordinates with a width of 80 and height of 100 and the rect of the viewport is at (70,90) with a width of 40 and height of 20 you index with [70-79][0-29] for the x coordinate and [90-99][0-9] for the y. This can be achieved by calculating the index with the following formula:
idx = (n+i)%80 (or%100) where n is the top coordinate(x or y) for the rect and i is in the range for the width/height of the viewport.
This assumes that one step of movement moves the camera with non fractional coordinates.
So this is your second alternative in a little bit more detailed way. If you only want to repeat the terrain, you should separate the contents of the tile. In this case the contents will most likely be generated on the fly since you don't store them.
Hope this helped.

Resources