in my game im having one Ccsprite for arrow, and one b2body for ball... im trying to throw ball at direction which is pointed by my arrow sprite. here is my code... i'm counting rotation of arrow sprite and then applying impulse to ball at that angle...
float totalRotation = arrow.rotation ;
ballBody->ApplyLinearImpulse(b2Vec2(10.0f+cos(totalRotation)*25.0f,10.0f+sin(totalRotation)*25.0f), eggBody->GetWorldCenter());
BUt, this not working exactly...ball is getting thrown in improper direction.
The rotation property of a CCNode (and CCSprite, which inherits from CCNode) is measured in degrees, with clockwise rotation being positive. The Box2D world uses angles measured in radians, with counter-clockwise rotation being positive, which is more conventional for a cartesian coordinate system. In order to provide the correct angle to a Box2D function, you will have to convert. In Cocos2D, the conversion goes like this:
float angle = - 1 * CC_DEGREES_TO_RADIANS(totalRotation);
The macro converts the totalRotation from degrees to radians, and you multiply by -1 because Box2D measures positive angles in the counter-clockwise direction, which is opposite of the CCNode rotation.
Related
I'll keep the question short.
I'm making a 2D game.
I have an object, but since it has many pieces, I just out it in a box to have it's hitbox.
If it's going up/down/left/right at perfect angles, I can check for collision because it's just about the left corner and then height and width.
How do I calculate if my mouseX and mouseY collide with it if it rotates at different angles.
Found a formula that x' = x sin A - y cos A or something, but didn't work.
Thank you!
I've already asked this question in a different way here; Swift-Setting a physics body velocity by angle but the three attempts to answer it were unfortunately not exactly what I'm looking for, although I'm grateful for what they taught me anyway. I decided that I should simply rephrase my question with an example and further explanation instead of perpetuating a discussion via comments. So here it is.
Imagine I have an SKNode positioned in the centre of the screen. Let's say this is a ball, so any rotation action on it is not visible. I would need a way to have a random angle selected, and have said SKNode rotate to it, and then continuously move in the direction determined by the aforementioned angle from its original position in the centre of the screen, until, say, the edge of the screen.
I know how to determine the random angle, have the SKNode rotate to it, and have it stop at the edge of the screen. Hopefully with this example what I need is clearer, a way to simply have an SKNode move forward, but on an angle determined by a single variable, and not a velocity determined by two, dx and dy.
Thanks in advance.
To do this, you just need to use some trigonometry!
When zRotation is between 0 and 90, you don't need to do anything to the angle except converting it to radians. Just call tan(radianAngle). Now tan will return how much should the node move in the y axis when it moves by 1 in the x axis. If you learned trigonometry before, you should understand what I'm saying. If you have not learned trigonometry, learn it. :)
Let's say the node's zRotation is 60 degrees, which is π/3 radians. Pass that into tan and you get √3. This means your dx and dy parameters in SKAction must be in the ratio of 1 : √3 in order to make the node move in the direction of 60 degrees.
When zRotation is between 90 and 180, you need to first subtract the angle from 180, then convert it to radians. Again, pass that to tan and the return value is how much your node should move in the y direction when it moves by -1 in the x axis. The dx : dy ratio is now -1 : tan(angleInRadians).
When zRotation is between 180 and 270, subtract 180 from that angle and convert it to radians. The dx : dy ratio is -1 : -tan(angleInRadians).
Lastly, a zRotation bewteen 270 and 360, subtract the angle from 360 and convert it to radians. The dx : dy ratio is 1 : -tan(angleInRadians).
Just before you convert the angle to radians, check if the angle is 90 degrees. If it is, please hard code the dx and dy because tan(M_PI / 2) is undefined.
In the project I am using IMU sensors to track real player's hand and apply the transformation values onto 3D hand inside Unity.
As IMU sets the Y axis orientation relative to magnetic north of the earth, in the game, hand initializes on specific direction.
What I want is to calculate the offset of IMU's given Y values and 3D hand's original Y rotation, so that I can subtract that value to the 3D model's Y rotation (that will seem like player's initial Y Rotation is the same as 3D hand's). Code would be: transform.Rotate(Vector3.up, offset, Space.World);
IMU sends Euler angles (does it well, as I wasn't able to get Gimbal lock)
As I understand, I need to find out angle difference between 3D Hand's initial rotation and IMU's given initial rotation on XZ plane (or through Y Axis)
How do I calculate the offset?
You can use Quaternion.FromToRotation to calculate offset, something like:
var offset = Quaternion.FromToRotation(Vector3.up, imuUp);
transform.rotation *= offset;
I'm an iphone developer, but this question is about geometry.
I have a simple rectangle (maybe a photo). The user touches this photo at a point and drags their finger to a new point:
http://dl.dropbox.com/u/792862/Untitleddrawing.png
How many radians I must rotate this rectangle to simulate a rotation given by the touches?
I'm assuming that you have a fixed origin for your rotation (the crosshair in your picture would suggest so) and the touch sets the other point.
First you need a method to figure out the angle of a line. The atan2 function (available in any well-equipped math library) figures out the angle between any line and the X axis. First figure out the starting angle:
startAngle = atan2(startY - originY, startX - originX)
And then the ending angle:
endAngle = atan2(endY - originY, endX - originX)
And then just subtract the two angles from each other to figure out your rotation:
angleToRotate = endAngle - startAngle
You might need to swap things around depending on which way your rotations work.
Use atan2 to convert the starting point and the ending point to angles, after translating each to correct for a non-(0,0) origin if necessary.
I am currently developing a simple Pong game for the iPhone. Currently using CGRectIntersectsRect for the collision detection and as for the deflection of the ball when it hits the paddle, I just multiply the ball velocity with -1 (therefore reversing the direction of the ball).
What I am trying to do is to make it so that when the ball hits the paddle, it checks whether how far is the ball from the center of the paddle, and increases the deflection angle the further the ball is away from the center of the paddle. (E.g. In this case, the ball will be deflected back at 90 degrees no matter where it came from, as long as it hits the center of the paddle)
How am I suppose to do that?
Any help would be greatly appreciated.
Thank you.
What you have given us are reference points (centre and edge). What we need are a reference line from which to measure the new angle. Additionally, what you are saying is not consistent and thus does not make sense.
I am guessing that what you are asking is a way to calculate the outgoing angle such that it is only a function of where it hit on the paddle. If it hit the paddle centre, then irrespective of the incoming angle, it will bounce off at an angle of 90 degrees to the paddle. If it hit the paddle edge, then irrespective of the incoming angle it will bounce off at an angle of 45 degrees to the paddle.
If so, then the following should do it (it is not the only way).
Assumption: The paddle shape is a rectangle.
Let L be the length of the paddle.
Let K be a constant such that L / (2 * K) = 1 / sqrt(2).
Let D be the distance from the centre of the paddle (may be + or -).
theta = pi/2 - asin (D / K);
This should give you an angle relative to the paddle.
Hope this helps.