Jump to content
  • Advertisement

WaZz86

Member
  • Content Count

    6
  • Joined

  • Last visited

Community Reputation

100 Neutral

About WaZz86

  • Rank
    Newbie
  1. Hello All, I am having trouble figuring out which car my character is trying to get into. These cars are in a List of sprite. I've used a for statement to add rectangles over all the cars and count a variable to get the right amount of cars in the index. ill post an example below but i dont like it this way because im just adding rectangles over existing ones and im not really controlling the car im in, as of right now i am only able to have my player turn invisable at these cars. when it comes to writing the code for the movement i need to know which car he is in out of the list so the car may be controllable. public void Players_selected_Car(GameTime gameTime,Players player, Cars pCars, CarManager carManager) { Rectangle playerRec = new Rectangle((int)player.myPlayer.Position.X, (int)player.myPlayer.Position.Y, 50,65); for(int p = 0; p < carManager.parked_Cars.Count; p++) { Rectangle parked_Cars86 = new Rectangle((int)carManager.parked_Cars.myCar.Location.X, (int)carManager.parked_Cars.myCar.Location.Y, 50, 70); if(playerRec.Intersects(parked_Cars86)) { currentState = Keyboard.GetState(); if (currentState.IsKeyDown(Keys.E) && !previousState.IsKeyDown(Keys.E)) { player.PlayersDriving = !player.PlayersDriving; } previousState = currentState; } } } as you can tell it is just drawing over each car in the list and i am intersecting with those rectangles. Does anyone know how i could find the index of the car my player is intersecting with? and what do you think is better a list of sprites or an array of sprites? Thank you to all who help
  2. WaZz86

    XNA 4.0 2D car simulation

    I decided to post the working code for all of those who need something just like this for their game or project.
  3. WaZz86

    XNA 4.0 2D car simulation

    I finally got it to work the way i intended it to. with the help of my professor public float mCarRotation = 0; // Used to rotate car sprite public float mPlayerRotation = 0; // used to rotate player sprite public bool PlayersDriving = false; // to determine whether the player is drving or not public int InorOut; // to determine whether the player is in the car or not public bool PlayersCoasting; // to determine whether the player has let go of the gas pedal bool Driving; // to know if the car is moving forward bool Reversing; //to know if the car is reversing float CarsAcceleration; // used to simulated the press of a gas pedal float minAcceleration = 0.2f; // the minimun speed float maxAcceleration = 2.2f; // the maximun speed float CarsReverse; // used to simulated the press of a gas pedal float minReverse = -0.2f; // the minimun speed float maxReverse = -1.0f; // the maximun speed protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); var newState = Keyboard.GetState(); KeyboardState keyState = Keyboard.GetState(); myPlayer.Rotation = mPlayerRotation + (float)(Math.PI / 2); myCar.Rotation = mCarRotation + (float)(Math.PI / 2); myPlayer.Update(gameTime); myCar.Update(gameTime); bool leftKey = keyState.IsKeyDown(Keys.Left); bool rightKey = keyState.IsKeyDown(Keys.Right); bool upKey = keyState.IsKeyDown(Keys.Up); bool upKeyup = keyState.IsKeyUp(Keys.Up); bool downKey = keyState.IsKeyDown(Keys.Down); // TODO: Add your update logic here if (upKey) { if (PlayersDriving == false) { //PlayerMove = 0; //PlayerMove += (int)(90 * gameTime.ElapsedGameTime.TotalSeconds); myPlayer.MoveBy(0, -2); //= new Vector2(0, 1); } else { minAcceleration += 0.10f; CarsAcceleration = minAcceleration; if (minAcceleration >= maxAcceleration) { Reversing = false; Driving = true; PlayersCoasting = true; CarsAcceleration = maxAcceleration; CarsAcceleration += (int)(100 * gameTime.ElapsedGameTime.TotalSeconds); } } } if (downKey) { if (PlayersDriving == false) { //PlayerMove = 0; //PlayerMove -= (int)(90 * gameTime.ElapsedGameTime.TotalSeconds); myPlayer.MoveBy(0, 2); } else { if (CarsAcceleration <= 0) { minReverse -= 0.05f; CarsReverse = minReverse; if (minReverse <= maxReverse) { Driving = false; Reversing = true; CarsReverse = maxReverse; CarsReverse -= (int)(105 * gameTime.ElapsedGameTime.TotalSeconds); } } } } //////////////////////////////////////////////////Left Keys////////////////////////////////////////////////////////////// if ((leftKey) || (rightKey)) { if (PlayersDriving == false && leftKey) { mPlayerRotation = (myPlayer.vec2Position.X - myPlayer.vec2Position.Y); myPlayer.MoveBy(-2, 0); } if (PlayersDriving == true && upKey && leftKey || PlayersDriving == true && downKey && rightKey) { mCarRotation -= (float)(1 * 2.7f * gameTime.ElapsedGameTime.TotalSeconds); } if (PlayersDriving == true && PlayersCoasting == true && leftKey) { if (upKey == false) mCarRotation -= (float)(1 * 2.0f * gameTime.ElapsedGameTime.TotalSeconds); } } ///////////////////////////////////////////////// Right Keys /////////////////////////////////////////////////////////////////// if ((rightKey) || (leftKey)) { if (PlayersDriving == false && rightKey) { //mPlayerRotation += ( myPlayer.vec2Position.X - myPlayer.vec2Position.Y); myPlayer.MoveBy(2, 0); } if (PlayersDriving == true && upKey && rightKey || PlayersDriving == true && downKey && leftKey) { mCarRotation += (float)(1 * 2.7f * gameTime.ElapsedGameTime.TotalSeconds); } if (PlayersDriving == true && PlayersCoasting == true && rightKey) { if (upKey == false) mCarRotation += (float)(1 * 2.0f * gameTime.ElapsedGameTime.TotalSeconds); } } if (upKeyup) { minAcceleration = 0.2f; } if (keyState.IsKeyDown(Keys.Space)) { CarsAcceleration *= 0.910f; PlayersCoasting = false; } if (myPlayer.BoundingBoxRec.Intersects(myCar.BoundingBoxRec) && newState.IsKeyDown(Keys.E) && !oldState.IsKeyDown(Keys.E)) { InorOut += 1; if (InorOut == 1) { PlayersDriving = true; } if (InorOut == 2) { PlayersDriving = false; InorOut = 0; } } if (PlayersDriving == true) { myPlayer.Position = myCar.Position; } if (Driving == true) { myCar.vec2Position.X += (float)(CarsAcceleration * Math.Cos(mCarRotation)); myCar.vec2Position.Y += (float)(CarsAcceleration * Math.Sin(mCarRotation)); if (CarsAcceleration <= 0.99f) { PlayersCoasting = false; CarsAcceleration = 0; } } if (Reversing == true) { myCar.vec2Position.X += (float)(CarsReverse * Math.Cos(mCarRotation)); myCar.vec2Position.Y += (float)(CarsReverse * Math.Sin(mCarRotation)); if (CarsReverse >= -0.99f) { PlayersCoasting = false; CarsReverse = 0; } } myPlayer.Location = myPlayer.vec2Position; myCar.Location = myCar.vec2Position; //PlayerMove *= .9f; CarsAcceleration *= 0.990f; CarsReverse *= -0.990f; oldState = newState; base.Update(gameTime); } P.S this is not a class project....
  4. [font=arial,helvetica,sans-serif]Hello All,[/font] I am having trouble keeping track of which sprite i want to control in game for eaxmple getting into a car sprite that is in a list of sprites and controlling it. i tried using a for statement but it didnt work the way i like ive been told to look into a foreach statement for this. ///////////////////////////// CollisionDetection.cs ////////////////////////////////////// public void Players_selected_Car(GameTime gameTime, Players player, Cars pCars, CarManager carManager) { foreach (Cars Cars in pCars) { currentState = Keyboard.GetState(); if (player.myPlayer.BoundingBoxRec.Intersects(pCars.myCar.BoundingBoxRec) && currentState.IsKeyDown(Keys.E) && !previousState.IsKeyDown(Keys.E)) { player.PlayersDriving = !player.PlayersDriving; } previousState = currentState; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////// CarManager.cs ///////////////////////////// public void LoadContent(ContentManager Content) { pcarTexture = Content.Load<Texture2D>("Cars Spritesheet"); this.initialFrame = new Rectangle(0, 0, 54, 70); this.frameCount = 0; pcarTexture.Bounds.Equals(new Rectangle(0, 0, 39, 50)); } public void Initialize() { parked_Cars = new List<Cars>(); setup_ParkedCars(); } public void SpawnCars(int space) { Cars doublestriped_GreenCar = new Cars(pcarTexture, new Vector2(700, 270), new Rectangle(0, 0, 54, 70), 0); Cars doublestriped_BlueCar = new Cars(pcarTexture, new Vector2(700, 320), new Rectangle(0, 78, 54, 70), 0); Cars Black_SUV = new Cars(pcarTexture, new Vector2(700, 390), new Rectangle(29, 196, 35, 63), 0); Cars Tan_SUV = new Cars(pcarTexture, new Vector2(700, 460), new Rectangle(29, 299, 35, 63), 0); } public void Draw(SpriteBatch spriteBatch) { foreach (Cars Cars in parked_Cars) { Cars.Draw(spriteBatch); } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////// Cars.cs ///////////////////////////////// public IEnumerator<Cars> GetEnumerator(); // Dont know how to use this properly public Cars(Texture2D texture, Vector2 location, Rectangle initialFrame, int frameCount) { myCar = new AnimatedSprites(location, texture, initialFrame, Vector2.Zero); previousLocation = location; currentWaypoint = location; } There is much more in those classes but i felt there was no need to post it for what i need help in. i dont know how to use GetEnumerator for my list of sprites Thanks to all those who help.
  5. WaZz86

    XNA 4.0 2D car simulation

    Anyone know a formula or something i could use for this??? making the sprite move forward in the direction it has rotated to in a 2D world.
  6. Hello, I am currently working on a XNA 4.0 2D game project where you the player would be able to get into a car and drive around in the little window for now. My problem with this is that i cant get the car to move forward in the direction it is facing after being rotated. So far it just goes up the Y axis or the wrong angle. i will post what i am working with in code for this functionality. //////////////////////////////////// Game1.cs ////////////////////////////////////////////////////////////////////// myCar.vec2Position.X += (float)(Math.Cos(mCarRotation)); myCar.vec2Position.Y += (float)(Math.Sin(mCarRotation)); myCar.Rotation = mCarRotation + (float)(Math.PI / 2); rotation += .22f / 2; if (leftKey) { if (PlayersDriving != true) { myPlayer.Location += new Vector2(-2, 0); } else { mCarRotation -= (float)(1 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds); } } if (rightKey) { if (PlayersDriving != true) { myPlayer.Location += new Vector2(2, 0); } else { mCarRotation += (float)(1 * 3.0f * gameTime.ElapsedGameTime.TotalSeconds); } } /////////////////////////////////// AnitmatedSprites.cs ///////////////////////////////////////// void UpdateRotation() { //rotation += .2f / 2; if (bRotateByPosition) { Rotation = (float)Math.Atan2(vec2Position.Y - vec2LastPosition.Y, vec2Position.X - vec2LastPosition.X); } } public void MoveBy(int x, int y) { vec2LastPosition = vec2Position; vec2Position.X += x; vec2Position.Y += y; UpdateRotation(); } I got some of that code from some tutorial i was learning off of online. So what i need help with is having the car move forward in the direction it is facing. P.S my math isnt that great i've taken trigonometry and calculus but i'm a bit rusty since then.. Thanks to those who help.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!