need help converting my XNA PC Game to Xbox 360 - c#

Need help, i googled and converted my project file to xbox 360, but i do not know the buttons and stuff.. to make it work on the game pad.
here is what i done so far
if (isAI)
{
Ball b = Game1.ball; //this is AI
if (b.Y > padMiddle)
moveDown();
else if ((b.Y + height) < padMiddle)
moveUp();
}
else
{
GamePadState currentState = GamePad.GetState(PlayerIndex.One);
if (mouse.Y < padMiddle) // I need to replace mouse with xbox360 stuff
moveUp();
else if (mouse.Y > padMiddle)
moveDown();
mouse.y was declared as MouseState mouse = MouseState.GetState();
i need to replace that with xbox 360 buttons can someone help?

You were using a click on the paddle's upper region and lower region to determine if it should be moved up and down. That's going to be a fairly difficult thing to translate exactly on the 360.
If you really want to do it the exact same way, please clarify but if you want to translate it into something that makes more sense you're going to want to use the Thumbsticks for determining whether something should move up or down.
if (isAI) {
Ball b = Game1.ball; //this is AI
if (b.Y > padMiddle)
moveDown();
else if ((b.Y + height) < padMiddle)
moveUp();
}
else
{
GamePadState currentState = GamePad.GetState(PlayerIndex.One);
if (currentState.IsButtonDown(Buttons.LeftThumbstickUp)
{
moveUp();
}
else if (currentState.IsButtonDown(Buttons.LeftThumbstickDown)
{
moveDown();
}
}
In the above code, it's detecting if the player is pushing up or pushing down on the left thumbstick of the Xbox360 controller and then moves the paddle up and down appropriately

Related

Unity Touch position not working

I have some issues with my Android/iOS game.
The concept is pretty simple (Portrait mode) :
If you tap on the left part of the screen, it bumps the ball to the right.
If you tap on the right part of the screen, it bumps the ball to the left.
The problem is, I've put this code in my controller script :
void TouchManagement()
{
for(int i = 0; i< Input.touchCount; ++i)
{
Touch touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began)
{
Debug.Log(touch.position);
if(touch.position.x > Screen.width/2f && timer == 0f)
{
BumpLeft();
}
else if (touch.position.x <= Screen.width / 2f && timer == 0f)
{
BumpRight();
}
timer = cooldown;
}
}
}
And no matter where I tap when playing my build on my phone, the ball goes to the right. I can't even debug.log touch position with Unity remote since my phone isn't recognized for some reason.
According to other forum posts and questions, this code should work (And works with other inputs in the Editor), so I've no idea what to do.
Thanks for your help !

XNA Smooth Downward Swoop Motion

(Before you read, I'm very nervous about posting here since my previous question got a lot of negative responses... Please try and be nice. I am a student and can't write "Perfect" code yet)
I'm currently trying to figure out how to make a mosquito (Texture2D) swoop downwards and then come back up to it's original position.
Currently the mosquitoes simply move left and right within the screen bounds, with no Y movement.
I've stepped it through a debugger and observed it's Y coordinate. My mosquitoes are indeed swooping down and then back upwards again... However, they do it so quickly that it isn't visible to the human eye.
Therefore I need a way to smoothly swoop them down so that it's actually visible.
I am currently unable to embed images into my posts, however I have made a GIF demonstrating the effect that I want.
Here's a link to my GIF
Things I've tried:
Copied the first line of my movement code (seen below) and changed it so that it would only affect the Y coordinates.
Result: Mosquitoes were still swooping too fast to see.
Changing my mosquitoe's velocity.Y to include a Y that isn't 0, so that my movement code will also change the Y position instead of just the X position.
Result: Game was stuck in an infinite loop since my movement code is found in the Update() function, and the code never got out of the loop so that it could update the position...
Here's the movement code I have in my Update.
The mosquitoes move all the way to the right, then all the way to the left.
internal void Update(GameTime GameTime)
{
position += velocity * (float)GameTime.ElapsedGameTime.TotalSeconds;
// Reverses the direction of the mosquito if it hits the bounds
if (position.X <= gameBoundingBox.Left ||
position.X + animationSequence.CelWidth > gameBoundingBox.Right)
{
velocity.X *= -1;
}
}
And here's the actual Swoop() code that I have...
internal void Swoop(GameTime gameTime)
{
float originalYPosition = position.Y;
bool currentlySwooping = true;
while (currentlySwooping)
{
position.Y++;
if (this.BoundingBox.Bottom >= gameBoundingBox.Bottom)
{
currentlySwooping = false;
}
}
while (!currentlySwooping && position.Y > originalYPosition)
{
position.Y--;
}
}
I don't ask questions on here too often, so I'm sorry if I'm using an incorrect format or if I've given too little information.
If you need to know anything else then please let me know.
There are many ways of implementing this, but this should do it.
float originalYPosition = position.Y;
int swoopDirection = 0;
internal void Update(GameTime GameTime)
{
/* Same update code */
if (swoopDirection != 0)
{
position.Y += swoopDirection;
if (swoopDirection == 1 && this.BoundingBox.Bottom >= gameBoundingBox.Bottom)
{
swoopDirection = -1;
}
else if (swoopDirection == -1 && position.Y <= originalYPosition)
{
swoopDirection = 0;
}
}
}
internal void Swoop()
{
swoopDirection = 1;
}
Basically, we have a variable swoopDirection that changes depending on the state of the swoop (1 when going down and -1 when going up). In the update method we check that swoopDirection != 0 (we are swooping). If we are swooping we add the direction to the Y axis and check that we aren't out of bounds. If we touch the bottom we set swoopDirection to -1 so we go up. If we are at the original position swoopDirection is set to 0 and we stop swooping.

RPG style collision boundary

i'm working on a small RPG game, and what i currently have is:
*Character with animation and basic collision;
*A 32x32 box which i test the collision on;
And thats all for now..xD
My character is 32xPixels wide, and 48xPixels tall, 32x38.
And i have Two rectangles, playerRectangle(for collision and movement);
And rectangleAnimation, for the character animation.
Well, my question is, how do i make a collision rectangle that only covers half of the character? Right now my rectangle is 32x48, so how do i make it 32x24, WITHOUT cropping the image?
It gives the illusion of being infront of something, for example a rock, or a tree.
I tried to make a seperate sprite, which is 32x24 and made a rectangle of it and drew two sprites in my player class, but that didn't work...
And my last question, should i make a seperate class, like CollisionHandler.cs for all my solid things, like walls, and boxes?
Here's my movement code for the player(without attempting to make a diffrent sized rectangle):
if (keyState.IsKeyDown(Keys.Left))
{
playerRectangle.X -= playerSpeed;
movingLeft = true;
movingRight = false;
}
else if (keyState.IsKeyDown(Keys.Right))
{
playerRectangle.X += playerSpeed;
movingRight = true;
movingLeft = false;
}
if (keyState.IsKeyDown(Keys.Up))
{
playerRectangle.Y -= playerSpeed;
movingUp = true;
movingDown = false;
}
else if (keyState.IsKeyDown(Keys.Down))
{
playerRectangle.Y += playerSpeed;
movingDown = true;
movingUp = false;
}
(I'm sorry that the code looks messy, i'm new to this site and i don't know how to structure it :/)
And my collision check(Which is in Game1.cs):
if(isColliding())
{
if (player.movingRight)
{
player.playerRectangle.X -= player.playerSpeed;
}
else if (player.movingLeft)
{
player.playerRectangle.X += player.playerSpeed;
}
if (player.movingUp)
{
player.playerRectangle.Y += player.playerSpeed;
}
else if(player.movingDown)
{
player.playerRectangle.Y -= player.playerSpeed;
}
}
I have a method in Game1.cs that's called "isColliding", which checks for collision in another class rectangle and my playerRectangle..
I'm sorry this is so long, but thank you in advance, and tell me if you need to know something else :)
i will write from my head... add this into your sprite class, it will be universal
public Rectangle BoundingBox
{
get { return new Rectangle((int)Position.X + offsetX, (int)Position.Y + offsetY, Texture.Width - offsetWidth, Texture.Height - offsetWidth); }
}
simply changing offsetX, offsetY, offsetWidth, offSetHeight you can adjust collision box. so if you wish collision on top left corner for 5x5 pixels. then offsetX=0, offestY=0, offsetWidth=5, offsetHeight=5
and collide code
foreach (Sprite brick in bricklist)
{
if (player.BoundingBox.Intersects(brick.BoundingBox))
{
// do some stuff
}
}
If you want to change the height of the player rectangle by half, all you do is just divide the playerRectangle.Y value by 2.
Use Vector2.Distance(playerPosition, objectToCheck);
if(Vector2.Distance(playerPosition,objectToCheck < player.rectangle.width/2){
//If the player is too close to the object you wish to check
//Do something...
}
For the collisions, i'd just check in your player's update method whether or not 'he' has collided with anything - using vector.distance^^
You're movement logic is bad:
You should be using velocity.
var velocity = new Vector2(0,0);
var moveSpeed = 5; //Whatever you want
if(player presses right){
velocity.X += moveSpeed;
}
if(player presses up){
velocity.y += moveSpeed;
}
this.position += velocity * Gametime.elapsedGametime; << Think Gametime is correct...
Var origin = new vector2(rectangle.width /2, rectangle.height/2); This will set the origin of the sprite to the center of the rectangle, as opposed to the top left.
Say we have two rectangles both 50x50... to check for a collision between the two you'd do:
if(Vector2.Distance(rectangle1.position, rectangle2.position < rectangle1.width/2){
//If the distance between the two rectangles (from the middle of the rectangle(the origin we just set is less than then half of the width of the rectangle... we have a collision.
}
Haven't used XNA in a few months, so you might have to update the origin in the update method of your classes...

How to do image collision in C# for Windows Phone 8 using Windows Phone App?

I have 2 images(bar and greenBall1). bar can be move up and down depends on the user response. While, greenBall1 is moving around the screen. I want to do an image collision if both the images touch each other, greenBall1 will change its velocity. The codes that I have for greenBall1 are as below.
private void OnUpdate(object sender, object e)
{
Canvas.SetLeft(this.GreenBall1, this.greenBallVelocityX + Canvas.GetLeft(this.GreenBall1));
Canvas.SetTop(this.GreenBall1, this.greenBallVelocityY + Canvas.GetTop(this.GreenBall1));
var greenBallPositionX1 = Canvas.GetLeft(this.GreenBall1);
var greenBallPositionY1 = Canvas.GetTop(this.GreenBall1);
var maximumGreenBallX = ActualWidth - this.GreenBall1.ActualWidth;
var maximumGreenBallY = 400 - this.GreenBall1.ActualHeight; //Improvise: Instead of 360, get maximum height of canvas
if (greenBallPositionX1 > maximumGreenBallX || greenBallPositionX1 < 0)
{
this.greenBallVelocityX *= -1;
}
if (greenBallPositionY1 > maximumGreenBallY || greenBallPositionY1 < 0)
{
this.greenBallVelocityY *= -1;
}
}
I don't see a reference to the bar object in your code. But the detection of the collision is much easier than the physics of handling the collision. There are several schools of thought to something as simple as Pong collision, and the way you choose to handle it depends on the gameplay you want.
Here is an easy way to detect and handle the collision, simply by negating the X velocity just as you are handling the wall collisions:
if(greenBallPositionX1 < leftBar.X || greenBallPositionX1 > rightBar.X)
{
this.greenBallVelocityX *= -1;
}
Keep in mind you might also have to take into account the width of the bar or the ball depending on where the coordinate is in relation to the image. For example:
if(greenBallPositionX1 < (leftBar.X + leftBar.Width) || greenBallPositionX1 > rightBar.X)
{
this.greenBallVelocityX *= -1;
}
You may also want to at this point move the ball away from the paddle one step to avoid the collision being detected more than once.
Hopefully this answers what you were asking, but if you were looking for a more complex reaction to the collision detection, then you may want to check out the following discussion on Pong type collisions here.
i think this might help you ....
.
Rectangle ballRect = new Rectangle((int)ballposition.X, (int)ballposition.Y, ballsprite.Width, ballsprite.Height);
Rectangle handRect = new Rectangle((int)paddlePosition.X, (int)paddlePosition.Y, paddleSprite.Width, paddleSprite.Height/2);
if (ballRect.Intersects(handRect))
{
// Increase ball speed
ballSpeed.Y += 50;
if (ballSpeed.X < 0)
ballSpeed.X -= 50;
else
ballSpeed.X += 50;
// Send ball back up the screen
ballSpeed.Y *= -1;
}
in this whenever the ball will collide with hand it will increase its speed and change its direction i think that is the think you are looking for as it is also a rectangular collision.
make a class which will give you value that wether two rects will collide or not
public bool Intersects(Rect r1,Rect r2)
{
r1.Intersect(r2);
if(r1.IsEmpty)
{
return false;
}
else
{
return true;
}
}
then you can use
if(Intersects(r1,r2))
{
MessageBox.Show("Collison Detected");
}

Create walls in XNA

So I'm creating a game in XNA using C#, and I have walls that are created in random positions, and I'm trying to stop things walking through them/be teleported to points on the screen when they hit them. (Note, Left and right now works, it's just the top and bottom)
if (collision(wallRect[k], wallColours, pacmanRect, pacmanColor, 0))
//Collision works, not an issue here
{
if (pacmanRect.Bottom > wallRect[k].Top && pacmanRect.Top < wallRect[k].Bottom)
{
if (pacmanRect.Right >= wallRect[k].Left
&& pacmanRect.Right < wallRect[k].Right)
{
pacmanPos.X = wallRect[k].X - frameSize.X;
//frameSize is the size of the pacman sprite
}
else if (pacmanRect.Left <= wallRect[k].Right
&& pacmanRect.Left > wallRect[k].Left)
{
pacmanPos.X = wallRect[k].X + frameSize.X / 8;
}
}
else if (pacmanRect.Right > wallRect[k].Left && pacmanRect.Left < wallRect[k].Right)
{
if (pacmanRect.Bottom >= wallRect[k].Top)
{
pacmanPos.Y = wallRect[k].Y - frameSize.Y / 8;
}
else if (pacmanRect.Top <= wallRect[k].Bottom)
{
pacmanPos.Y = wallRect[k].Y + frameSize.Y / 8;
}
}
playSound(collisionSoundInstance);
}
That is the last point in the game loop where pacmanPos is updated. So how would I make it so that the walls are actual walls, and you can't walk through them?
if (pacmanRect.Bottom > wallRect[k].Top && pacmanRect.Top < wallRect[k].Bottom)
Does this make sense? I don't think it's possible for Pacman to be above the top of the wall, but simultaneously below the bottom of it (unless maybe your y-coordinate system is positive in the down direction, and even then the logic still looks fishy)
I would recommend drawing one of the states out on paper, then going through your code in your mind line-by-line given what you see on your paper. For example, draw the state of Pacman's bottom-most edge overlapping the wall's top-most edge. Then walk through your code and see what happens.
If you have a game which is restricted to two dimensions you should use Box2D which makes your life much easier.
See comment if you want to stay to your own code

Categories