I make one box2d game and I have some problem based on touch.
I desired one box2d body object moves on touch with speed / velocity.
I have an Ant box2d body with revolute joint and I try to move that box2d body object with touch and speed of touch. But I have no idea for that.
Ant body
I am also a new developer in Box2d.
please help me.... for solving this issue...
CGFloat velocity = ccpSub(position, previousPosition);
CGFloat speed = ccpLength(velocity);
Related
I have been creating a physics Model Newton Cradle(pendulum) in Unity 3D, but have been facing difficulties in rotating and zooming camera around object, applying momentum conservation for pendulum objects.
I'm not sure if this will help but based on the title of this statement[?] I'm guessing you want to rotate the camera around a target. This is some simple code that may help you on your way.
target=GameObject.FindWithTag("Cradle").transform;
transform.LookAt(target);
transform.Translate(Vector3.right * Time.deltaTime*2);
edit the number after deltaTime to change the speed and the Vector 3 for the translation. Obviously change "cradle" to whatever the tag of object you want the camera to focus on.
I have this classic situation of a pinball game
All objects are physicsBody with rights collision masks and so on.. they collide perfectly.
The problem is that they only work when they are static...If I try to rotate a paddle on its anchorpoint, there is no collision with the ball and the ball falls down, passing through the paddle.
I thought it was a problem of speed but I think that during the rotation the physicsBody simply doesn't work.
Do you have suggestions?
Thank you so much.
That's exactly the problem you get when rotating a static body, it's not going to act physically correct.
For example static bodies have no force, no velocity - if you move or rotate it, it will just be there at the new position and new rotation without pushing any dynamic bodies around. Only if a dynamic body now happens to be intersecting with the static body will the physics engine try to resolve the collision, usually in a brute-force manner (aka "get me outta here").
Therefore the paddle has to be dynamic if you want it to move, accelerate and spin the ball. Both paddle and ball may need to have continuous collision detection enabled as to not lose any precision in the paddle motion applied to the ball, and vice versa.
i just started working with SpriteKit and encountered a problem:
I want to have an object, i call it playerController, that follows the players touch and a puck, with can be hit by the controller and will in that case bounce of the controller.
Somehow like an AirHockey game.
I constructed 2 SKNodes, with texture and physics, so they bounce of each other properly if gravity is on or if the controller is moving by some velocity.
BUT if the controller is moved by the following action the puck does not bounce of.
SKAction *moveTo = [SKAction moveTo: CGPointMake(touch.position.x ,touch.position.y) duration: 0.1];
[self.controllerPlayerOne runAction: moveTo];
Why? I already searched in the documentation and in several tutorials, but none addressed my problem. Maybe this is wanted by the SpriteKit developer, but how is in this case the realisation of following an touch?
Hopefully someone can help.
Edit:
Currently, i got the controller puck behaviour by setting the controllers position to the touch position and calculating the speed by using the last and the current position of the touch, each time the touch moved.
This is working pretty good.
This is by design. The same behavior can be observed in cocos2d btw.
The thing is: move actions move the sprite, not the physics body. Even if the physics body does follow the sprite, the move action overrules the physics body motion, at best putting it where the sprite is.
Instead when using physics bodies you must move them by changing the body's velocity, you can no longer reliably use the move actions. The physics body then moves the sprite.
I'm applying an impulse to a sprite and I currently have an arrow behind the sprite to show which direction the sprite will be shot. But what I want to do is scale the arrow on its x axis to show the power of the impulse. How would I do this? I know how to scale on the x axis but how would I relate this to the touch, to do the impulse in using cgpoint in touchbegan and touchmoved.
I want to do this OR
show a guide line that will show the path the sprite will travel when the touch has ended.
Thanks
I'm using spacemanager for the physics.
I managed to scale the arrow to touch, I just got the distance between the touch, made it smaller and limited the scaling.
Heres my code
arrow.scaleX = ccpDistance(birdPosition, pt1)/50;
if (arrow.scaleX > 1.0) {
arrow.scaleX = 1.0;
}
If anyone knows about the guide line Id still love to see that nd compare the two.
Thanks
I have a hierarchy like this:
Scene
- gameWorld (CCLayer Z:0)
- player (CCSprite Z:1)
- spriteWorld (CCNode Z:0)
- bgSprite (CCSprite Z:0)
- enemy (CCSprite Z:0)
The spriteWorld gets rotated and the bgSprite gets moved around so any CCSprites placed on bgSprite has a rotated and moved coordinate system compared to the gameWorld coordinate system.
I need to detected collisions between CCSprites placed both on the gameWorld and bgSprite. Now I have seen this post here:
Cocos2D CCNode position in absolute screen coordinates
which can give me each sprites position relative to the screen, but how do I then calculate or get the bounding box for these sprites? Those bounding boxes are rotate both via the spriteWorld rotation but each sprite also rotates on the bgSprite.
Sounds like a lot of crazy math to me and I am a fool at math, so I really really need help here.
Thank you
Særen
Indeed, cocos2D has no direct support for collision detection, so you would need to do all your calculations on your own.
Anyway, you can use a framework like chipmunk-physics or Box2D to do that for you. The basic idea is that you associate a body/shape to your sprite and then setup a collision callback that will be fired when the collision is detected, without worrying about the math...
Have a look at this article about using Box2D just for collision detection.