How do you control a player character in Bullet Physics? - c++

I am not sure how you are supposed to control a player character in Bullet. The methods that I read were to use the provided btKinematicCharacterController. I also saw methods that use btDynamicCharacterController from the demos. However, in the manual it is stated that kinematic controller has several outstanding issues. Is this still the preferred path? If so, are there any tutorials or documentations for this? All I found are snippets of code from the demo, and the usage of controllers with Ogre, which I do not use.
If this is not the path that should be tread, then someone point me to the correct solution. I am new to bullet and would like a straightforward, easy solution. What I currently have is hacked together bits of a btKinematicCharacterController.
This is the code I used to set up the controller:
playerShape = new btCapsuleShape(0.25, 1);
ghostObject= new btPairCachingGhostObject();
ghostObject->setWorldTransform(btTransform(btQuaternion(0,0,0,1),btVector3(0,20,0)));
physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
ghostObject->setCollisionShape(playerShape);
ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
controller = new btKinematicCharacterController(ghostObject,playerShape,0.5);
physics.getWorld()->addCollisionObject(ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
physics.getWorld()->addAction(controller);
This is the code I use to access the controller's position:
trans = controller->getGhostObject()->getWorldTransform();
camPosition.z = trans.getOrigin().z();
camPosition.y = trans.getOrigin().y()+0.5;
camPosition.x = trans.getOrigin().x();
The way I control it is through setWalkDirection() and jump() (if canJump() is true).
The issue right now is that the character spazzes out a little, then drops through the static floor. Clearly this is not intended. Is this due to the lack of a rigid body? How does one integrate that?
Actually, now it just falls as it should, but then slowly sinks through the floor.
I have moved this line to be right after the dynamic world is created
physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
It is now this:
broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
I am also using a .bullet file imported from blender, if that is relevant.
The issue was with the bullet file, which has since been fixed(the collision boxes weren't working). However, I still experience jitteryness, unable to step up occasionally, instant step down from to high a height, and other issues.

My answer to this question here tells you what worked well for me and apparently also for the person who asked.
Avoid ground collision with Bullet
The character controller implementations in bullet are very "basic" unfortunately.
To get good character controller, you'll need to invest this much.

Related

vtkResliceCursorWidget rotate both axes

I am using a vtkResliceCursorWidget in a VTK app, and I want setup a useful behavior: when I move a side (axe), I want to being moved both axes.
See the images from below:
Actual behavior:
Desired behavior:
I have found inside of vtkResliceCursorWidget representation, a method that fit my needs:
SetManipulationMode(vtkResliceCursorRepresentation::RotateBothAxes)
but the issue is that though I have used, it simply do nothing:
vtkResliceCursorRepresentation* pRep = reinterpret_cast<vtkResliceCursorRepresentation*>(resliceCursorWidget[1]->GetRepresentation());
pRep->SetManipulationMode(vtkResliceCursorRepresentation::RotateBothAxes);
where resliceCursorWidget is a vtkResliceCursorWidget, taken from here:
Example
Somwhow I expect of this, because on SetManipulation method remark write quite clear: "INTERNAL - Do not use Set the manipulation mode. This is done by the widget", even this method is a "public" method.
Could you guide me in order to able to move both axes (of vtkResliceCursorWidget) on the same time ?
Thank you.
With the CTRL key as modifier, the behaviour will be as desired.

How to exchange parts of a 3D model in OpenGL?

I wanted to know if anyone has idea how to do the following:
we have a 3D model of a character ..
Now I need to make, is that this "character" have interchangeable parts ...
For example, if you want to add a helmet or armor to the model, I guess I should replace the vertex of the torso or head vertex for the armor or helmet vertex.
That's where the problem comes, as I can know where "the part" starts and where finish and start another part? (in blender so I have separate the character in "groups", arm, foot, head, etc..
I think a possible solution:
i had thought to create an object for each "part" and load as separate models (arm on one side, head for the other and so) and handle all parts as "one model".
but I think it is not right... you can tell me what is the correct way ? tell me your experience in this case (if you know any tutorial on this subject. thanks!)
i Working in C ++ and OpenGL 3+ on Windows
For modeling using Blender.
Ive never done it from scratch in opengl but in unity what you do is, if you want for exemple a sword in your characters hand, you give the sword model the position and rotation of the bone that is in the characters hand, and the object(sword) will movea cordingly to the hand.
You can obviously do the same for armor, hair and all kinds of cosmetics.
If you want to change body parts like an arm or hand the prenciple is the same, you can for exemple use a tranparente texture for the part you want to switch and simple put the new hand as mentioned above.

Project1.exe Has Stopped Working. I cant seem to find what the heel is wrong with my SDL Code

All right, i apologize right away because the code is actually kind of big. The thing is, i am working with SDL, learning actually. I had managed to create animations, but i decided i wanted to go a little deeper. This is how the Code works
App - Uses: This has the main loop, the Init function, the Render Function and the whole softwares is originated from here.
Animation - Uses: This will contain and declare all of the functions in the Class Animation. It only works when a bool variable called Running is true.
Surface - Uses: This will Load files onto surfaces, Make certain color transparents and Draw images or part of images onto the MainSurface.
Events - Uses: This has all of the Virtual Functions to Events
Object - Uses: This is the big part. It was working perfectly. Before i would have a Surface Called Yoshi on the Main Class, and i would interact with the animation using that Surface. I thought that the method of having the "Character" on the main Class was a little bit confusing and it would cause me problems when having Multiple images on the screen. So therefore i decided to create a class called Object, which would have its own SDL_Surface, its own location on screen, Its own variable decided which frame it is. What i did was that i created an object to This class on the APP Class... Something like
Object MainCharacter;
So then i would be able to manipulate that Character alone, and if i ever needed to have another Object on screen i would just use
Object *SecondCharacter;
etc... So then the OBJECT Class would call the Animation. APP Class would render all of the images, by a function somewhat like this
MySurface::OnDraw(Surf_Display, MainCharacter.Img_Surface, MainCharacter.RetrievePosX(), MainCharacter.RetrievePosY(), MainCharacter.RetrieveFrameByID(0), MainCharacter.RetrieveFrameByID(1), 64, 64);
Well... But the thing is: The code stops working. This is far from a finished code, but i would like to fix this issue right away, so i can finish only after it already has a base.
Here are all of the files: Some of them dont have too many lines. I also wrote some comments to help anyone who may be able to help me.
HEADERS:
Animation.h - http://pastebin.com/v4REcmBd
Object.h - http://pastebin.com/KdeamZTG
Events.h - http://pastebin.com/dpQ7zwpG
CApp.h - http://pastebin.com/5zZYwDjv
Surface.h - http://pastebin.com/S66ChQ3f
SOURCES:
Surface.cpp - http://pastebin.com/KRgAkHpw
Object.cpp - http://pastebin.com/RtDRwVj4
Events.cpp - http://pastebin.com/dpXn8Rh3
OnRender.cpp - http://pastebin.com/AqgMTacb
OnLoop.cpp - http://pastebin.com/UAeAt7y6
OnInit.cpp - http://pastebin.com/1iiZVeyK
OnEvent.cpp - http://pastebin.com/AjRXnKC1
OnCleanUp.cpp - http://pastebin.com/cv0M11nV
CApp.cpp - http://pastebin.com/L0jhfWY8
Animation.cpp - http://pastebin.com/0BZhh6TG
All right those are it. Some of them are really short. Well, if anyone can help me finding where is my mistake i would be glad. And with that aside, i would really like to get an opinion on the structure of the software, do you think what i am doing is practical? What Design do YOU use when creating SDL Software or Games???
Having been told in the comments it happens on line 12 of Source.cpp which says
Res = SDL_DisplayFormat(Temp);
and googled SDL_DisplayFormat for you, I find this manual page
Near the bottom it says:
Newbie hint
You have to call SDL_Init before using the SDL_DisplayFormat function.
If you don't, your program will crash with an access violation.

Retain cycle with CCAction and CCCallFunc/CCCallBlock

I'm in the final stages of releasing my first game, and after running Instruments:Leaks & Allocations, I see that I have a leak in my code caused by a retain cycle. I am using Cocos2d 2.0, and compiling my app with ARC, and I should mention that I started the project pre-ARC, and used the Xcode refactoring tool to convert it. My game has several animated objects per screen, each of which has a small number (1-7) of animated "variants" of that object (i.e. the barn opens to show a horse once and a zebra another time). I have a class that represents each animation, and another class for each variant. The variant creates a CCAnimation from a sequence of frames, and then creates an action which will be run whenever a touch event is received in the correct region. This action is what is causing my retain cycle. My declaration for the action ivar looks like this:
#interface AnimationVariant : NSObject
{
#private
CCAction* _action;
...
}
#property (readonly, nonatomic) CCAction* action;
...
-(void) setupActionWithMask:(int)mask
cycles:(int)cycles
hasScale:(bool)hasScale
scale:(float)scale
masterScale:(float)master_scale
animationFrames:(NSArray*) frames
duration:(float)duration
andBlock:(VoidBlock)block;
#end
In the implementation of the setupActionWithMask method, I build up an NSMutableArray of CCActions, actionList. The sequence of CCActions varies depending on args, but usually it looks something like this:
[actionList addObject:[CCScaleTo actionWithDuration:0.0f scale:scale]];
[actionList addObject: [CCAnimate actionWithAnimation:animation] ];
[actionList addObject:[CCScaleTo actionWithDuration:0.0f scale:master_scale]];
[actionList addObject: [CCCallBlock actionWithBlock:block]];
And I create the action like this:
_action = [CCSequence actionMutableArray:actionList];
The consuming class creates an AnimationVariant instance, sets its properties, calls setupActionWithMask, and passes in a block it wants executed when the action completes. When the consuming class wants to play the animation variant, it does so like this:
[self runAction: variant.action];
I tried declaring _action as:
CCAction* __unsafe_unretained _action;
which of course broke the retain cycle, but the action is destroyed, and is no longer around when it's needed (which is what you would expect, since __unsafe_unretained does not retain). I know __weak is the recommended solution, but as I am targeting iOS 4 and up, I don't think it's available to me.
I had another retain cycle in my code, exactly like this one, also caused by retaining (automatically with ARC of course) a CCSequence containing a CCCallFunc/CCCallBlock. I solved that one by just re-creating it whenever I needed it, which I could also do in this case, but these animations are triggered maybe a couple hundred times in the whole game, so I was hoping to follow the recommended Cocos2d Best Practices and retain the actions.
Thanks!
Retaining actions is not best practice. It's not even good practice. Though it comes heavily recommended by many, quite unfortunately.
Retaining actions works in many cases, but fails in others causing objects to leak. I'm guessing your case may be one of those.
Since you're targeting iOS 4 you can't use weak references. But you should probably reconsider unless you have to target the remaining few 1st and 2nd generation devices. Otherwise, google for iOS 5 adoption rate. The handful of devices that haven't been updated yet are well below a reasonable threshold, in particular if you consider that those users probably don't buy (many) apps (anymore) anyway.
Since you meantioned CCCallFunc, make sure you don't use them and replace with CCCallBlock. CCCallFunc are not safe to use with ARC, in particular whenever you have to __bridge_transfer cast a data object to void* (also bad practice).
There's always the chance that the necessary bridge cast back to the original object never occurs, and then ARC doesn't get the chance to clean up that object. With CCCallFunc this can happen when you run a call func action but the action is stopped before the callback selector is called, for example by changing scenes or stopping the action/sequence.
Cocos2D is also prone to retain cycles if you don't follow this rule:
any node should only retain another node that is one of its children or grandchildren
In all other cases (ie node retains (grand)parent or sibling node) you must make sure to nil those references in the -(void) cleanup method. Doing so in -(void) dealloc is too late because the object will never get to dealloc when there's a retain cycle.

OPENGL ARB_occlusion_query Occlusion Culling

for (int i = 0; i < Number_Of_queries; i++)
{
glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[i]);
Box[i]
glEndQueryARB(GL_SAMPLES_PASSED_ARB);
}
I'm curious about the method suggested in GPU GEMS 1 for occlusion culling where a certain number of querys are performed. Using the method described you can't test individual boxes against each other so are you supposed to do the following?
Test Box A -> Render Box A
Test Box B -> Render Box B
Test Box C -> Render Box C
and so on...
I'm not sure if I understand you correctly, but isn't this one of the drawbacks of the naive implementation of first rendering all boxes (and not writing to depth buffer) and then using the query results to check every object? But your suggestion to use the query result of a single box immediately is an even more naive approach as this stalls the pipeline. If you read this chapter (assuming you refer to chapter 29) further, they present a simple technique to overcome the disadvantages of both naive approaches (that is, just render everything normally and use the query results of the previous frame).
I think (it would have been good to link the GPU gems article...) you are confused about somewhat asynchronous queries as described in extensions like this:
http://developer.download.nvidia.com/opengl/specs/GL_NV_conditional_render.txt
If I recall correctly there were other extensions to check for the availability of a result without blocking also.
As Christian Rau points out doing just "query, wait for result, do stuff based on result" might stall and might not be any gain because of that, depending on how much work is in "do stuff". In fact, doing the query, waiting for it to round trip just to save a single draw call is most likely not going to help at all.