So I need some help with my C# XNA top-down 2D "wave spawn" game. Basically, I have a player that has the functionality of shooting a laser at enemies that spawn in waves. The problem that I'm looking at fixing right now is my enemy collision code with a stationary non-rotated wall (rectangle)
I need to detect which side the enemy hits the rectangle on so he can properly get around the rectangle and to the player. (Example: if the enemy is blocked from the right side of the wall, move up the wall until he can get around the wall)
Here is the code I have to detect the angle between the enemy and the stationary wall when collided:
// I am blocked, find angle to find which side I'm blocked from
centerOfWall = wall.CenterOfWall;
Vector2 difference = centerOfWall - centerOfEnemy;
// Getting the angle of intersection between the two rectangles!
angle = Math.Atan2(difference.X, difference.Y);
// Converting angle to degrees!
degrees = angle * (180 / MathHelper.Pi);
I guess what I'm asking is, with this angle, can I find the side at which the enemy hits the wall? Like if the angle is less than 90 degrees but greater than 0 degrees I will be hitting from the Left or something like that.
If this doesn't work, can someone help me in finding another way that would work?
Thanks,
Johan
Related
I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.
It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.
What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.
Someone have any code ideas?
Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a Rigidbody with drag 0.
Also uncheck gravity to get the following result:
Basic Force Script:
After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.
public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);
Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.
For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.
If you'd rather it go in a random direction:
calculate the magnitude of the ball's velocity sqrt(vx^2+vy^2)
find the angle the wall is facing
pick a random angle within 180 degrees of that angle
use trigonometry to get the x/y components of that angle, and multiply by the magnitude.
Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.
I want to write a simple foosball game in unity. To rotate the players I am mapping my mouse movement to the rotation of the players:
float mod = (Input.mousePosition.x - RotationSpeed) * RotationSpeed;
rb.transform.eulerAngles = new Vector3((90 - mod) , 90, 90);
with mod being a delta to my mouse position. However by doing so I can't kick the ball as I teleport through it by setting the exakt angle. So my question is: how to map the rotation in a way that I actually am able to apply force to the ball, so that it can be shot?
Edit more information:
By moving my mouse to the left, my players rotate clockwise. E.g. my mouse is at x position 300 , then the rotation of the player will be set to 300 times a step (here called "Rotation Speed"). Counterclockwise in the other direction. As in a typical foosball game I want to kick the ball by flinging my mouse to one of the directions. However the ball gets stuck / does not move much when they collide. The reason for this is probably, that by setting the euler Angle directly, the players "teleport" through the ball and don't kick it back. So I need some kind of an instant smooth motion to where my mouse is.
I'm making a maze whit Unity3D where a ball can roll and find the out way. Because the ball can be hide after a wall, I want to rotate the camera to a better position in function of the direction of the ball.
Take this example: the ball is rolling in to you (in the direction of the black arrow). So you can see or beter can't see, is where the ball is rolling to. So the camera must turn to the other side of the ball. If the ball rolls away from you must the camera turn to the original location.
The problem is now, I know how I can replace the camera but not in function of the direction? Can anyone help me with this? I'm just starting with Unity3D. Language behind I use C#.
Here is another situation where it is better to rotate the camera. (up: is scene, below game mode).
You can use this to set the camera position behind the ball based on the velocity and then the rotation in the direction of the ball
Vector3 offset = new Vector3(1,1,0);
transform.position = ball.transform.position - ball.GetComponent<Rigidbody>().velocity / ball.GetComponent<Rigidbody>().velocity.magnitude + offset;
transform.LookAt (ball.transform.position);
I am making a game, using a onscreen/virtual joystick. The joystick it self works fine.
What I want is when you turn the joystick, a bullet is fired in that direction. I got that working too. Now the problems arises when shooting a bullet when the joystick is not at the edge/radius.
When the joystick is at the edge/radius, the bullet move at maximum speed, exactly what I want. But when the joystick is, lets say, somewhere between the radius and the center of the joystick, the bullets move slower. This is logical because of the radians and stuff.
But it is not what I want. What should happen is that the bullets always move at the maximum speed.
Does anyone know how this could be achieved? I am completely stuck at this.
Thanks!
EDIT: since I still dont understand this at all, basically what I want is that I have a point in radians within a circle and I want to get the position of that point at the edge of the circle.
So let's say I have this Vector2 in radians.
Vector2(-0.3, 0.3);
and the radius of the circle is 40.
The length from the center of the circle to the Vector2 point would propably be somewhere around
radius = 40 * 0.3 = 12
Now what would the Vector2 point be when the length is 40?
I can't seems to figure this out. Trigonometry is definetely not my strongest point.
I fixed it! I just multiplied to localPosition of the joystick with a very large number, so that the localPosition woul fall outside of the radius. Then I just use clampMagnitude to get it at the edge of the radius. Now I get the correct behaviour.
Im making a 2D game where the player controls a tank.
I can make the tank, and all, but whats really messing with my mind is how to make it rotate accordingly.
I want it to behave just like the Wii game, Tanks.
Fixed directions, and with no real front and back on the tank.
Driving up, then left should make it rotate to the left.
Driving up, then down should not make it rotate, just drive the other direction.
I red a tutorial a while back about some way to do that by dividing the degrees into 2 180 degree parts. But i have simply not been able to find that damn site again.
I hope you guys are able to understand what im trying to say.
Thanks in advance :)
I assume you're drawing your tank as a sprite? In that case there's an overload of the SpriteBatch.Draw method that allow you to specify the rotation angle around the origin.
SpriteBatch.Draw overload
Here's an example on how to use it from MSDN
The example above will keep rotating your sprite, so you will need to add some custom logic so it will only rotate it according to keyboard input. Here's a simple example on how to check for keyboard input. So add logic that checks if the right or left button has been pressed, and update the rotation angle if they have. If it's the up or down button that has been pressed you simply modify the position of your sprite.
I hope it makes sense, otherwise just let me know.
I think what you're looking for is simply the best way to minimize the rotation of the tank, modulo 180 degrees.
I would use the angle between the desired movement direction and the tank's current direction to start. Make sure this is the minimum angle, then compare that with the angle between the tank's current direction + 180 degrees. Something like:
// smallest angle between the current direction and the desired direction
minAngle1 = Math.Abs(Math.Min(tankAngle - desiredAngle, desiredAngle - tankAngle));
// smallest angle between the opposite direction and the desired direction
oppositeAngle = (tankAngle + 180) % 360;
minAngle2 = Math.Abs(Math.Min(oppositeAngle - desiredAngle, desiredAngle - oppositeAngle));
// get the smaller of two to rotate to
if (minAngle1 < minAngle2) {
// we know that we should rotate the current direction to the desired direction
} else {
// rotate the opposing direction to the desired direction
}
Note you'll need to play with your rotation signs to ensure you're rotating the right way. Also, I've assumed you know your rotation angles, if you have vectors you can simplify this a little bit by using the dot product between the two vectors instead of the angle for comparisons.
Is your problem with the direction of movement based on the angle they have rotated?
Vector2 moveDir = new Vector2(Math.Cos(rotation), Math.Sin(rotation));
position += (moveDir * speed);
Speed here would be a number for how fast you want to move in that direction. position is another Vector2 for the position of the sprite. As Tchami says you can draw it with the rotation using the SpriteBatch.Draw overload. Rotation for the the Cos and Sin methods should be in radians but I think Draw should be in degrees if I remember correctly. MathHelper.ToRadians(degrees) and MathHelper.ToDegrees(radians) should solve that.
There is lots of XNA tutorials and examples on the site http://creators.xna.com/en-US/education/catalog/