Knockback effect with top down graphics on box2d - java

I'm currently making a top down gfx actiong RPG on java, using jbox2d. Basically I want the enemies to recoil back after they get hit by the player (with top down graphics I don't have friction or gravity). I tried with restitution, applying impulse and setting linear velocity but I didn't get what I expected: the enemies teleport to the destination istantly and if they are near the wall they get ported out of the map. How can I fix this and what is the best to do it in your opinion? thanks a lot

I don't know how you are using jbox2d and c++ at the same time...
Regardless, if your enemies are represented physically by b2_dynamicBodys then you probably want to apply a linear impulse and set the linear damping of the enemy body to a value greater than zero. Linear damping works like air drag - The faster the object is moving, the larger the force applied in the opposite direction. Applying a large linear impulse and setting a high linear damping will cause your enemies to fly away from your hero with a very high initial velocity, but they will come to rest very quickly.
I hope this helps!

Related

libGDX gravity and constant acceleration doesn't work properly

I am trying to create a simple Flappy Bird clone but in my own way ( so it's not a complete clone ).
However I am having problems applying a constant downwards acceleration to the Bird. When I create a World it says that it gets a gravity variable but that's not gravity I think ? It should say something like velocity because that is all it does. My Bird is falling downwards at a constant speed. And I believe most of you know how gravity works. When I use functions like applyForceTo I basically get the same.
I have already my own simple implementation of gravity but I wanna use libGDX to its fullest and practice with it.
I found the answer myself and the solution is pretty simple. The objects I render, the whole "System" is way too big for Box2D and its properties to make any difference on the display. Everything accelerates accordingly but you can't see it. So I scaled down every object so it had properties of a couple of pixels and adjusted the camera. Now I can get very high accelerations and velocitys.

Box2d helicopter physics

I am developing java game using box2d for my physics, I have got helicopter, ex:
I reduced gravity by setting:
body.setGravityScale(0.03f);
So it acts bit realistic (is affected by gravity only little bit, floating in the air)
To move it, down/up left/right I have controller, thats how I control my helicopter:
body.applyLinearImpulse(new Vector2(pValueX * 3, pValueY * 3), mainBody.getWorldCenter());
Where pValueX and pValueY are 1 or -1 (directions up/down left or right)
It works good, but now I am trying to achieve more realistic effect, when moving helicopter left/right I wanted to tilt it little bit so it works like real helicopter, but could not find proper way how to do it, I have tried applying force in different part of the body, but it makes my helicopter rotating 360 degrees if keep pressing left or right.
This question is old, but in case it's still relevant, I created a helicopter using JBox2D (which pretty much maps directly to Box2D). For tilting left/right (i.e. forwards/backwards relative to the pilot):-
heli.applyTorque(TURN_TORQUE);
or
heli.applyTorque(-TURN_TORQUE);
This rotates the heli, and then if the player wants lift:
Vec2 force = new Vec2();
force.y = (float)Math.cos(chopper.getAngle()) * -1;
force.x = (float)Math.sin(chopper.getAngle());
force.mulLocal(ROTOR_FORCE);
heli.applyForceToCenter(force);
What you can do is, just define two constants as maxForceLeft and maxForceRight. When you press left apply some force on the cockpit part of the helicopter and keep comparing it with the maxForceLeft,once it reaches that value stop applying the force.Do the same for the right button by applying the force on the tail rotor part of the helicopter.In this way you can avoid rotating it 360 degrees.Depending upon the kind of effect you want for your helicopter you can apply the forces in either upward or downward direction.
http://www.iforce2d.net/b2dtut/rotate-to-angle
What you need is rotating the body to a desired angle..
This is a great tutorial to achieve this.
I hope this would help.

Box2D Having gravity affect different masses

If I give bodies different densities/mass, they still fall with the same speed. I know about the fact that in a place without air-resistance, mass doesn't affect falling speed.
But then, how do I logically make, let's say a balloon and a brick, fall at different speeds? The closest way I could think of is to use setGravityScale to set this all..
The best method to simulate the air speed reduction effect in box2d is to use "damping".
see: http://www.box2d.org/manual.html
"Damping is used to reduce the world velocity of bodies. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together."
"Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. I generally do not use linear damping because it makes bodies look floaty."
bodyDef.linearDamping = 0.0f;
bodyDef.angularDamping = 0.01f;
One option is to disable gravity and apply the accelerations you want each frame yourself. That's the route I went in my game. Box2d's built in gravity is ok for quick simulations, but it isn't very customizeable.
Once you've disabled gravity, you have to decide which acceleration formula to apply to your objects. There are several different models of fluid resistance (check Wikipedia), so you'll have to experiment and pick the one that looks the best.

algorithm for visualizing gravity distortion (2D)

I'm working on an Android game and would like to implement a 2D grid to visualize the effects of gravity on the playing field. I'd like to distort the grid based on various objects on my playing field. The effect I'm looking for is similar to the following from the Processing library:
Except that my grid will be simpler- 2D, and viewed strictly from the top, as if looking down at the playfield.
Can someone point me to an algorithm for drawing such a grid?
The one idea that I came up with was to draw the lines as if they were "particles"- start at one end of the screen and draw the line in multiple segments, treating each segment as a particle, calculating the effect of gravity at each segment's location.
The application is intended to run on Android.
Thanks
I would draw each line as a separate segment, as you mentioned. If the grid is sparse, it might be fastest.
If you are viewing the grid from above, you would need to calculate x and y coordinate displacements. The easiest way would be to actually do displacement along the z axis and then fake perspective with x_result = x/z and y_result = y/z . You set z=1 and make sure to vary it only relatively slightly (+- 0.1 for instance).
Your z should be proportional to the sum of 1/(distance to the sphere)^2. This simulates how gravity works - it tapers off with square of the distance. Great news - square of the distance means to calculate delta_x^2 + delta_y^2 - so you save yourself that square root calculation == faster.

Collisions and velocity, how do I predict hits that will take place between this update and the next?

I'm doing simple collisions on moving, coloured pixels. If their velocity get's higher than 1, the pixels may pass through something in the static world I'm trying to collide with.
How do I compensate for this?
This C++ example uses a vector based approach to predicting the paths of particles undergoing elastic collisions. This Java example is similar, rewinding to the start of a collision between particles when overlap is detected. In each, the critical element is separating the model from the view. By doing so, it's possible to iterate the model at 1 pixel/tick and update the view at a different, variable rate.
The article 2-Dimensional Elastic Collisions without Trigonometry may also be helpful.

Categories