I'm creating box2d game and trouble is as follows:
Two fighters located on arena, top view. Fighters has weapons like sword in theis hands. I want to fighter bounces in hit direction when damaged by opponent, but fighters has same physical characteristics and applied impulse is not sufficient to bounce fighter considerably.
How can I increase applied by box2d impulse in same (correct) direction? I overrided PostSolve method, but I don't understand how to affect to b2ContactImpulse passed to method. I think I just need increase value of that impulse, but I dont know how. Could someone explain me how to do it? Thanx!
Related
I'm trying to make a basic first person shooter game with Bullet and OpenGL. I'm having the issue of my rigid bodies not colliding at high speed.
My bullets will go straight through any other rigid bodies that I have, such as walls. Reducing velocity to less than 10 does result in collisions, but this is too low for a moving bullet. The bullet also moves insanely fast (I know it's a fast moving bullet, but sometimes I can't even see it, not sure if that's expected).
I'm thinking that it's to do with how I'm stepping the simulation? Reading up on it has left me confused. How can I make it so that my objects will always collide (at least, when going reasonably fast), and if possible, is there a way to slow the simulation down whilst maintaining the correct bullet velocity etc. so that I can actually see the bullet moving and colliding?
Here are some approaches to solve:-
It is copied from How can I avoid missing collisions for fast moving objects? - an official FAQ
smaller timesteps
extruding the object along the motion
ray cast to the new position
swept collision test (convex cast, linear cast)
continuous collision detection, including rotational motion
Please read the link for more detail. It is not a trivial issue.
One important thing to ask before try anything: do you really need high speed object?
It is not free (cost more CPU).
Here is another useful link (less useful though) : https://gamedev.stackexchange.com/questions/11961/how-can-i-enable-ccd-in-bullet-physics
I'm trying to get my box2d box to bounce when hitting another box2d box. The physical properties to both objects are set thus..
fixtureDef.density=1;
fixtureDef.friction=0;
fixtureDef.restitution=0.9;
And when I apply this line (which is meant to be the correct way of doing things) I get no bounce.
myElementsArray[0].myPhysics.myBody.ApplyImpulse(newVec, myElementsArray[0].myPhysics.myBody.GetWorldCenter());
But when I change the 2nd, part to some random vector, it bounces
myElementsArray[0].myPhysics.myBody.ApplyImpulse(newVec, new b2Vec2(20,20));
Which I don't get, what am I doing wrong?
The "bounce" you are seeing is probably a bit deceptive. I'm not a physics guru, I only know enough be dangerous as my main focus is game programming. Anyway here goes, the first statement uses the center of mass to apply the force to. When you are calculating a "bounce" using restitution, in most physics libraries it would very simply grab the largest restitution between the two collision objects:
restitution = Max( bodyA.restitution, bodyB.restitution )
Then disregarding torque, the new differential final velocities with respect to the restitution is:
–restitution * (bodyA.initialVelocity - bodyB.initialVelocity)
This is also more standardly written as:
–e(V1i - V2i)
Why is this important? Because this is probably the reason you are not seeing a bounce, the two initial velocities (without torque) are zeroing out. Now the second call to ApplyImpulse() uses a coordinate that is not the center of mass of the body. Because of this, Box2D is going to attempt to apply an angular velocity to the body. With that in mind a new series of equations will be used. I'm fairly confident that is where the problem resides, but someone else might correct me or know more about it.
I am using Box2d (libgdx) to implement a BMX bike with its rider. The bike is one body and each limb of the rider is one body. When airborne during a jump, the player should be able to control not the rider but the bike. Since the rider is connected to bike at the handlebar and at the pedals, the limbs will follow the bike, until the arms/legs are fully stretched at which point the movement of the bike is restricted.
Can anyone suggest a good way of using joints (probably) to achieve this? Both the bike and the rider should be affected by gravity, but the rider body should not collapse due to gravity. It's OK to have the limbs stiff and unaffected by gravity (their internal positions, that is), but the limbs must be affected by the bike. Preferably the internal positions of the rider limbs should be affected for example when the bikes does a heavy landing against the ground, but this is second priority.
I could set angle limits on the limb joints to prevent it from collapsing, but then I guess they will not budge when I try to move the bike.
If the limbs follow the bike, how can the arms/legs get fully stretched?
I think first you'll need to set limits for the limb joints to stop them from turning inside-out in violent collisions (eg. elbow joint turns backwards).
Then you could determine a target angle for each joint in the resting position, and each time step, use the joint motor to make the joint move to the rest position. If you make these joint motors just strong enough to hold the rider upright, but not too much stronger, then he should be affected by a heavy landing.
I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside.
Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically.
I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not.
My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.
Check the corners of your bouncing ball with four hitTestPoints. If it succeeds, then do hitTestPoints from the fence with shapeflag set to true.
There may be better solutions as I do not know the performance impact of shapeflag, but combined with the corners optimization I think it will be good, but I'm also interested if there is a better way.
Math will be your friend here. Do a quick search for circle-line, or point-line collision (here's one: Circle line-segment collision detection algorithm?).
What you do is run through your array of points, creating lines. So line 1 will be points[0] and points[1], and line 2 will be points[1] and points[2]. After that you check each line against your ball (if you want proper collision that will work no matter the frame rate, then you create a ball line, which is the line that the ball has travelled along between the last frame and this one). Do your collision detection against the ball line and each line made from your points (there's tons of line-line collision detection algos on the web). What you'll get out of an algorithm like that is the time the collision takes place in the current time step, as well as the normal of the colliding line, which will give you the reflection angle.
If you don't know Vector math, then learn it, it'll make your life a ton easier. Again, there are tons of implementations of a Vector2 class on the net.
You can arbitrarily remove parts of the wall as needed by just ignoring those points in your check.
Another lazy solution would be to use a physics engine like Box2D http://box2dflash.sourceforge.net/ or Nape: http://code.google.com/p/nape/ - it might be overkill for what you want for your game, but hey, it's easy.
For bonus points, another technique which might be easier for you is the Separating Axis Theorem, which is used in the flash game N: http://www.metanetsoftware.com/technique.html
I'm trying to create a multi-level dungeon adventure in Flash CS4. The layout is an instance created of a symbol called Level, within the symbol are multiple wall subsymbols (wall), instances of Wall. There is a collision routine to stop the player walking through the walls, called from Wall.As.
Level is drawn about the centre point (0,0).
When I create an instance on the stage of Level (level), the collision tester is using the xy coordinates for the walls drawn about 0,0, not the "real" xy where it's appearing on the stage.
So what I need to know, is how to "update" the xy for each wall subsymbol with the live stage information, overriding the XYs drawn in the parent. It has to be updated unfortunately (I can't keep it static), as the levels are big so have to scroll.
Thanks for your advice.
With all due respect forget your approach, you're reinventing the wheel for nothing and probably to end up getting worse performance. What you need is pixel-perfect collision detection and probably including basic physics so already we're talking a huge amount of work. If you want to build levels in a design way for a game, use this, it'll blow your mind how awesome/easy/cool this is:
http://www.gotoandlearn.com/play.php?id=135
Its always a guess when trying to answer questions like this, as there are a lot of unknowns. That being said, in programming, there are always more than a few ways to solve a problem. Examine your collision detection routine - if you worked with hitTestPoint, and the point that was being tested (mouseX,Y or your main actor) with localToGlobal, you likely wouldn't need to test for the x,y variables of your collision objects. Read up on those two subjects and this question might be rendered moot.
At any rate, you could update relative coordinates in your Wall.as instance by leveraging globlaToLocal:
public function get curLoc():Point
{
return globalToLocal(new Point(this.x, this.y));
}
and retrieve them from your parent class as a point you can then test against:
trace(_wall.curLoc);
Hope that helps
I suppose you could accomplish what you're trying to do by manipulating the transform property of the wall symbols, but honestly I would concur with Ascension Systems and just abandon your collision testing routine for something different.
You may not need to go all out with a physics engine for your game, in which case just use hitTestObject to do the collision detection.