I need implement trampoline-like effect like in this question and apply a force to a body when it collides with trampoline.
But how can I detect this moment?
Or maybe can I just create a body which can't move by gravity or other bodies, but has a trampoline effect?
Do you want to create more force than provided by restitution?
Well you can do this all.....
One way is set restitution as 0.9 or 1(of both the bodies in contact)... the other way is get those bodies by tag and then apply linearImpulse on the body you want trampoline effect....
I hope this helps
Related
I have basic rectangle node with physics body, I want to find middle pint of it, because, I want to both ends of rectangle react different to collision with another objects.I was looking for answer on the Internet, I find that I can use CGGeometry, but I don't have knowledge how to do it. How can I do that?
Why not just add a physics body on each end of your rectangle as a child. Then you can just detect if the child physics bodies collided without any complicated math
I'm pretty new to Box2D and cocos2d. I'm trying to do something which I thought would be pretty simple, but it is turning out to be more difficult than I had expected and I cannot find a working answer anywhere.
Basically, I want to move a b2body and rotate it to a certain point with animation.
I can get it to the correct position and rotation with:
targetBody->SetTransform(b2Vec2(10.0f,1.0f),10);
But I have no idea how to animate it there over time. I tried using cocos2d animation on the sprite used for the body, but that doesn't do anything. Any ideas?
There are a couple of ways you could do this.
You could use SetTransform every time step to update the position of the body gradually over time, in effect performing the animation yourself. The drawback with this method is that the body is 'teleporting' to the new position rather than moving, so it has no momentum, which means you can get odd results if it hits anything along the way. Still, if you know it will not hit anything or does not need to react to a collision this would be ok.
The other way is to SetLinearVelocity and SetAngularVelocity to give the body proper movement. This will keep the results of a collision realistic, and you don't need to keep updating anything every timestep. On the other hand, you will need to detect when the body has arrived at the desired position and then set the velocities back to zero, otherwise it will just keep moving. For dynamic bodies you will also need to counter gravity somehow, either by setting the gravity scale of the body to zero, or by applying an upwards force to balance gravity, or by changing the body type to kinematic during the move.
In general, you use Box2D to simulate the physical behavior of objects in relation to each other. The rules of mechanics implemented by Box2D dictate how your cocos2d CCSprites move if you continuously update the translation and rotation of your sprites according to their corresponding Box2d b2Body. You will have some kind of repeatedly invoked tick: method in which you step your Box2d world along in time, and in which you update your sprite positions according to simulation results of Box2d.
This pattern corresponds to b2Bodys of type b2_dynamicBody. Physical laws dictate the motion of the body in this case, and your sprites will follow these simulation results. This is why setting a conflicting position of a sprite by means of a CCAction or even directly will be undone almost instantaneously with the next tick:.
Solution 1: kinematic body type
However, there do exist other modes for a b2Body, and one of these is b2_kinematicBody in which the translation is no longer governed by the world but by the velocities or angular speeds you dictate through setters. So it would be one solution to work with body type b2_kinematicBody as in:
b2BodyDef bdef;
bdef.type = b2_kinematicBody;
With this you would manipulate the velocity and angular speed of a b2Body explicitly. In this case, Box2d is still responsible for moving bodies around, but according the velocities you dictate, and without any force effects of collision or gravity applied to this particular body. Also with this strategy, your CCSprites will follow b2Bodys. Setting conflicting positions for your sprites directly or by applying a CCAction would not make sense for the same reason as described above.
Solution 2: decouple sprites from Box2d
An alternative way to animating sprites would be to fully decouple those sprites from Box2d. In this case, you would simply not have any b2Body that governs the position of the sprite you are going to animate. Now, in this case, only you will dictate the position and rotation of your CCSprite, i.e. directly either through its position property or by applying a CCAction.
How can I move body ball in box2d like a volleyball without accelerating or dumping (with a constant speed).
Do I need a special formula for this?
In Box2D you move an object with forces. You can apply impulses or a linear force.
You can apply a impulse doing:
myBody->ApplyForce( force, myBody->GetWorldCenter() );
Or a force by doing:
myBody->ApplyForce(force, myBody->GetWorldCenter());
Note than a force is a b2Vec that you can construct doing:
b2Vec force = b2Vec2(0,50);
This force will only push the body up.
If you need a parabolic trajectory then you can create a force that has the component x and y greater than 0:
b2Vec force = b2Vec2(50,50);
Then the physics engine will do the rest.
You can also move to a specific position although I dont advice you to do that.
If you want more information about forces then follow this link.
I want to slow down the speeds of objects moving with phyics...I want them to move in the same way but to slow down there speed.I hope u understand my problem.thanks
There are multiple ways to do so, depending on what you want to achieve. The simplest way is to set a linear damping value in the body in question. See the documentation here.
Another way, also quite simple, is to apply a force at every frame in the contrary direction as the object moves. You can alter the force applied by the speed the object moves in the direction. See the applyForce method here.
you change the value of gravity .That is use physics.setGravity(0,value).Adjust the speed as you want
Use physics gravity to slow down ur objectphysics.setGravity(-2,10)
Read this website:http://developer.anscamobile.com/reference/index/physicssetgravity
how can i perform animation on my moving object....
and how can i control collisions when some of objects are allowed to collide and some not...i,m beginner to corona and coding as well ...i look all the examples i know there r answers of my questions but i cant understand that properly...so any one help me??
In Corona when you create a Physics body you can specify the body type: static, kinematic, and dynamic.
Static bodies don't move, and don't interact with each other; examples of static objects would include the ground, or the walls of a pinball machine.
Dynamic bodies are affected by gravity and collisions with the other body types.
Kinematic objects are in-between Dynamic and Static bodies but doesn't response to gravity
For more information check these links: http://developer.anscamobile.com/content/game-edition-physics-bodies and http://developer.anscamobile.com/reference/index/physicsaddbody
By "animation" I'm going to assume you mean that the graphic changes. In that case you should use a Spritesheet:
http://developer.anscamobile.com/reference/sprite-sheets
As for collisions, there again I'll make an assumption because your question is a little vague. I interpret your question as wondering how to setup collisions where some objects don't collide with each other, and for that you want to set category and mask bits:
http://developer.anscamobile.com/content/game-edition-collision-detection#Collision_categories_masking_and_groups
It is very easy to make a animation by using corona sdk.If using sprite sheet animation u have use this link http://docs.coronalabs.com/api/library/display/newSprite.html.