How can I optimize rigid body collidsions with Ogre3D and BulletPhysics - bulletphysics

I am developing some games with my friends, using Ogre3D and Bullet Physics.
I managed to get our game character moving (just simple run and turn).
I guess I can implement 'jumping' by this class:
btCollisionWorld::ClosestRayResultCallback res(btFrom, btTo);.
However, my big issue is collision optimization.
I tried:
adding 100~500 rigidbody objects (after we changed these to mapobject in our game)
one staticplanecollision object (ground)
one character rigidbody object
FPS for just moving on plane is 60~80 = ok. But when my character collides with any other rigidbody object, FPS is going down to 7.
I have no idea that fix this problem.
I am not useing DynamicCharacterController or KineticCharacterController.
Character is controlled by setLinearVelocity() and rigidbody->getWorldTransform().setRotation(quat) function
Some more information:
PhysicsManager::GetInstance()->getDynamicsWorld()->stepSimulation(evt.timeSinceLastFrame,8);

I've had this issue before, I don't know if that's your case, but we were implementing Damned (game), and when we modeled the kitchens we were using the stoves' meshes as collision objects (btDbvtTriangleMeshShape), and everytime the character got close to a stove the FPS went down...
The solution was to use a simple collision shape for each stove (like btCompound with a few boxes), and the FPS issue went away :)
Hope your issue is similar to this so you can solve it by reducing the complexity of the collision shapes you're using.
If this isn't the case, please, show me the code you run when handling your character (also, KinematicCharacterController is highly recommended, and moving from Bullet to PhysX is even more recommended ;D).
Best.

Related

Box2D world.step() delay at first 2 runs

I'm programming a game using ActionScript 3.0 and included Box2D classes for its physics. It's a maze/labyrinth game having a lot of walls and a ball inside.
In my main fla when I call the maze.create(), the maze is created (visually and physically) and it will dispatch an event so I know when it's done working and then I call my frameHandler which calls another function from my maze class every frame and the big delay accrues exactly at world.step() in it. BUT THE THING IS that it lags only the first two times this function runs!!!
The reason I notice this lag, is that I've got another object starting to move according to mouse position in the same frame handler.
The reason I'm sure the world.step() is causing it, is that everything works fine when I dont call it.
I've seen many codes using Box2d, some had more objects than i have and I know that I've created my b2World and all the objects correctly, similar to all the Box2d tutorials and stuff BUT THEY DONT LAG AT ALL. Its just mine lagging and all!!
Do you have any Idea or similar experience?
Do you have any suggestion in general how to deal with heavy functions?
PLEEEEASE +.-
A maze will have a lot of fixtures in close proximity making a very dense dynamic tree (the method Box2D uses to optimize collision checks). There is unlikely to be any way around this. Perhaps you could just call Step a few times after adding the static walls, but before you add the dynamic ball bodies, and consider it part of the loading process. At least the lagging movements will not be visible to the player.

2D Platform Game using hitTestPoint:glitches

So I created my own code for a 2d platform game (like Mario). I decided to use the hitTestPoint method to handle my collisions. It worked smooth as butter, and everything was working perfectly. The character could move up and down ramps perfectly.
My next step in creating my game would be to change my character (which was a block at the time) into a human character that can go to frames labeled "walk", "jump" and "stand". After creating variables for the two character states "run" and "stand" I coded the character to go to frame label "run" if the left or right keys are down, and "stand" if the character is not running, jumping or falling. Then the code was working okay, but there were quite a few problems that were occurring such as the character randomly running through walls and teleporting up ramps.
I still have both versions (working and glitched). Other than the character movie clip changing frames, the code was not altered. I have absolutely no idea why the character is acting like this now. If anyone has experienced a problem like this before or knows what the problem behind this is, please help me out!
Drake Swartzy
It's very likely your samples are not frequent enough (with the implementation you're using) to detect a collision.
With a low enough frequency, you'll update the position of your character, and then test whether it's overlapping the target. This means (if you're going fast enough) you could completely miss the collision.
A better approach may to use raycasting. See How do I handle collision detection so fast objects are not allowed to pass through walls?
Thanks so much for the help Atriace! Sorry, I actually just discovered the problem.... The code was glitching up because when I changed the scaleX value of the character (when the character turned directions), it also effected the points which were nested within the character. These points were used to handle character collisions. Thus that's why the character was acting strangely when it came to X values. However, your answer was definitely a possibility in a situation like this. Once again, thank you for the help.
Cheers,
Drake Swartzy

Efficient collision detection in AS3

I have a problem with a game that I'm doing. I basically have objects that are in a map and I have to check for each of them if they collide with the walls (and then do something). Since was working with AS2, I thought about doing the same way: I drew a picture with only the walls, so with only rectangles and everything else in between is transparent (does not exist, then the floor for example). In AS2 I put the image to the screen, let's call it wall, and then I did a hitTest to wall with every object. That is for instance, the object was actually on the image, since that the transparent parts were part of it, but the function was testing only on the visible parts, and so with the walls. So it worked.
Now in AS3 there is no HitTest but hitTestObject, which I used, and I do for example wall.hitTestObject(object). The problem is that this function is as if it doens't see the transparencies, and the objects while not touching the walls collide with them!
I found the PixelPerfectCollisionDetection that actually solves the problem but it is huge and heavy so in my case, with so many objects to be tested (at least 60) at each frame, the game slows down a lot!
What I need is a function like hitTestObject (i don't need a lot of accuracy!) that take care of the transparent parts of an image.
How can I do?
As mentioned in the comments, physics/game libraries will have this code built-in for you and should work out of the box.
But if you want to build it yourself, or even introduce your own optimizations, the first step (which is very inexpensive) is checking for bounds collision using entirely built-in functionality of DisplayObject.getBounds and Rectangle.intersects (though you must do so in a consistent coordinate space, i.e. the stage):
if (obj1.getBounds(stage).intersects(obj2.getBounds(stage)) {
// Cheap bounds intersection is true, now do pixel-perfect detection...
}
Then if the bounds check is true, perform the pixel-perfect collision detection.
It seems that BitmapData.hitTest is your best bet - see a blog post by Mike Chambers.
Prior to this method, if you're interested in neat techniques, there was a method outlined by Grant Skinner in his blog. It's quite a clever algorithm using built-in bitmap routines (aka, fairly fast), creating a BitmapData only as large as the overlapping region (or even scaling that down), and drawing the two objects into specific channels of the bitmapdata, then using BitmapData.getColorBoundsRect() to determine if there are any pixels touch. I'm guessing BitmapData.hitTest is faster, but it'd be fun to compare.
I ran into the same problem and to be honest i found the easy way to get rid of that is just generating a "mask" layer for the collisions. You can always place this under your background so it doesn't show, or change the transparencies and whatsoever. Do this in Flash, and after "covering" with rectangles (or whatever) the collisions, just select them all and make that a movie clip.
I'm guessing since you made the symbol in Flash, it obviously knows that even if the symbol consists of several individual drawings or whatever, it's not just an image.
For me this worked fine .

Detecting point where two objects collide

I'm totally beginner in Flash and Actionscript so sorry if my questuon is stupid.
I have to make a platform game - I'm planning to write something like Icy Tower - my character is jumping from one platform to another to get to the top of a tower of something. And here is my problem - I want to allow the character to jump onto a platform only from the top, but if he collides with whe platform from the bottom, or from the side, I want him to bounce (I hope you understand what I want mean).
So, to do something like that, I need to be able to detect where exactly do these two objects collide - the only solutions that comes to my mind is to keep the coordinates of every platform in some array and compare them with the characeter's position evertime he jumps, but it just doesn't seem right. Is there an better way to solve this problem?
I will be grateful for any advice.
PS. Sorry for my English
You can make use of a game library like flixel, or flashpunk. They are both great.
Essentially, you will need to iterate over all objects that your character can collide with. You can make use of .hitTestObject() to roughly determine if your character is colliding with any of the objects. Thereafter you can then get the coordinates and dimensions of your character on the stage and compare them to the coordinates and dimensions of the hit object to determine directions.
So in psuedo code
for each (var platform:DisplayObject in platforms) {
if (character.hitTestObject(platform)) {
if (collideFromBottom || collideFromSide) {
// allow movement
}
if (collideFromTop) {
// stop downward velocity
}
break; // might as well, as you have detected the collision
}
}
CollideFromBottom would be comparing the top of your character with the bottom of the platform. Remember that as Sprites, both player and platform have [x,y] properties and [width,height], allowing you to determine coordinates.
P.S. this question should have been asked at gamedev.stackexchange.com
I'd recommend you to use a Physics Engine like Box2D(
behold the examples), instead of creating some hitTest colision based game from scratch.
The probability of you having serious code/design problems and leaving the project aside are big.
There's a lot of good tutorials on internet, here a good resource - Using Box2d to Create a Side Scrolling Game:
Part 1
Part 2

Flash CS4 / AS3 symbol within symbols

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.