iphone box2d moving a body in opposite x direction - iphone

I have two balls in my game. Both are affected as is by gravity (i.e. if I tilt the device right the balls roll to the right etc...).
What I want is that the second ball will be rolled in the opposite direction. (So if I tilt the device right the ball will roll to the left !)
Since gravity affects my bodies direction I cannot find a way to make the second ball roll in reverse.
What is the efficient and correct way of doing this ?
Thanks

Just use body->applyForce. Use GetLinearVelocity of bodyA and apply the opposite of it to bodyB + (gravity * -1)... (or something like that)
OR
create two worlds one with gravity set to b2Vec2(0.0f, -12.0f); and the other to b2Vec2(0.0f, 12.0f); then just do opposites in your accelerometer method to change each worlds gravity
If you are only going to ever have just two balls, option one would most likely be best.

Related

Unity 3D object with Rigidbody sliding

I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.

SpriteKit move node with physics rather than updating position

My main character moves by touching and holding him and then moving your finger left or right to move the character. To do this I just simply update the node's x position (walks left and right on a flat surface) in touchesMoved() with the x position of the touch location, and apply an animation depending on the direction he's moving.
I want to kind of take this to the next level and accomplish the same effect, but using physics, so that when I'm done moving him and release my finger, he may slide a little bit in the direction he was moving given the speed I was moving him at, if that makes sense. Does anyone know how I can accomplish this effect?
Would I have to do as I'm currently doing and update the position as it moves, but also apply a force/impulse at the same time? Kind of confused on how to approach this
Moving the physics body via force, impulse, or velocity will automatically update the player position.
So you will have to play around with the correct way to accomplish your goal. But based on this, what I would suggest is replace your .position code with .physicsBody!.velocity code in your touchesMoved. Then, on your touchesEnded, you can apply a bit of an impulse to give the player that little bit of an "on ice" effect to keep them going a tad.
You will need to experiment with how much velocity you want to get the character to move at the correct speed, and then you will need to play with the impulse figures as well to get it just right.
This could get a bit tricky though, in touchesMoved... because at some point you will want to reset the velocity to 0 (so they stop moving when your finger stops).
To do that, you will need to to use the .previousLocation from your touch object, and compare the distance of X moved. if the distance X moved is >0 (or some deadzone threshold) then apply the velocity; if the deltaX is 0, then set the velocity to 0.
This may actually be more complicated than just using .position to move the character, then having the character slide a bit with physics on touchesEnded.
You will have to play with it to get it right.

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.

Slope limit on player movement

I use Unity3D and I have a player with a rigidbody. I add force tto the body for moving the player. My player walks over a terrain but is able to walk up mountains that are to steep to climb. I want to limit the player so it cannot walk up a slope that is to steep.
I know there is a CharacterController component that has this functionality, but I have to use the rigidbody, so I want the same but on my rigidbody.
I can get the normal of the triangle I am standing on, and calculate its angle, but I cannot seem to make the player stop moving up the slope. Only make the player stop moving (which makes the player unmovable once it hits a angled slope)
Any ideas how to solve this problem?
It's difficult to answer without more details on how you're using the physics engine. How/Are you using friction? What angle are you applying the force? Is it always horizontal or at the angle of the floor? Does the player have a mass?
Anyway I can think of a few ways to solve this
Go the pure physics route. Using player mass, friction, force angle, gravity, etc. Get the physics to handle these situation for you. This may take a fair amount of time and programming.
Keep the rigid body but fake the forces. Scale the force you are applying to the body of the player with the angle of the triangle the player is on. You can either use trigonometry to work out what you should apply or your own mapping. By your own mapping I mean set an angle where 0 force is applied (say 45 degree) and do a linear(or non linear) scale on the force applied so on flat ground force is 1 and at 45 force is 0.
Don't use rigid bodies. There is a reason most games don't use rigid bodies to control characters. It's hard and complicated and most of the time not worth the time it would take. Of course I don't know the details of your project so if this isn't an option, fine.
Hope that gives you some things to think about.

2D Game control question - rotation & movement

I was wondering if anyone could help me get started or pointed in the right direction. I'm looking to make a simple driving game using core animation. I'm a pretty good obj-c programmer, but when it comes to movement and math, I fail. I want a steering wheel to control the direction of the car, and a forward/backward to control acceleration & deceleration. If anyone can help me with the steering wheel code, I would be greatly appreciated! So basically I need help making a circle that can rotate with 1 finger drag, and i'll pass it's transform values to my car view ( i think i can handle the accel/deccel code) Any takers? :) Also, I have 2 invites for dribbble.com to give away, anyone who helps me, id be glad to give one out to.
grady, totowtwo, thanks for both your answers, I appreciate it. I;ve gotten to the point where I can rotate a "steering wheel" and it will rotate my "car", I also got it to move forwards and backwards. Just need to moving to be more realistic... Here is a link to the xCode project, http://www.cl.ly/7VBU, kept it very simple so if anyone looks at it - it will be easy to change / add code. So, if anyones wants to look at it,and help me make the movement more realistic, i'd be forever in your debt! :) thanks!
A starting point would be to recognize that a typical car steering wheel has 900 degrees of movement, whether you want that for the finger wheel or not is a design decision. The wheels in a car typically point about 30 degrees off +z at maximum rotation of the wheel. You could use a simple linear transformation from steering wheel angle to wheel angle.
From there, Car delta angle is a function of wheel angle times forward distance. As the car moves forward, you can imagine tracing the tangent of a circle with an infinite radius when you are pointing straight ahead, and a circle of smaller radius (~4x car length) when steering wheel is full tilt.
Of course, feel free to discard the design rules of typical vehicles. Give your users fun drive trains like holonomic robots have!
I am no expert but you could probably do something like:
CGPoint centerOfWheel;
CGPoint currentTouchPoint;
float radians = atan2(currentTouchPoint.x-centerOfWheel.x, currentTouchPoint.y-centerOfWheel.y);
then apply the transformation to the wheel ... of course you will want to make the transformations cumulative on the view over a distance, as continued turning of the wheel will result in continued turning.
also to make it feel more real, and less responsive you may want to use a lesser degree of turn, so that turning the wheel in once circle doesn't result in a whole circle turn of the vehicle. you will have to play around.