Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Collision Class 2 player game
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DrummaBoyFB




PostPosted: Sat Sep 11, 2010 12:57 pm   Post subject: Collision Class 2 player game

Hi everybody.
I am in the process of making a game and it's looking pretty good so far, but the problem is that I don't know how to create the collision class.
The problem is that I set the movement of my players in the variables x and y but I don't know how I can set them apart differently according to each player
so that when they reach close to each other they collide.

Example : If I want Cloud and Axel to collide , I don't know how to do so because both of them use the variable x and y as their movements.
so how do I make the x and y different according to each player?

If you don't understand please tell me and I'll try and explain it in a diffferent way.

Here's my Player Class :


c++:

// Player.cpp
#pragma once
#include "Player.h"
#include <string>
#include <sstream>
#include <cstring>
#include <string.h>

Player::Player (string name , bool player, int StandFrames , int WalkFrames, int AttackFrames,
  int SAFrames, int WeakFrames) {
  wcounter = 0;
  acounter = 0;
  sacounter = 0;
  wecounter = 0;
  player1 = player;
  Name = name;
  walkcounter = 0;
  atkcounter = 0;
  secondcounter = 0;
  weakcounter = 0;
 
  for (int i = 1; i <= StandFrames; i++) { // note i put <= to in standframes
    stringstream str;
    str << "Final Battle\\" << name << "\\" << name << "Stand" << i <<".bmp";
    StandImage [i] = load_bitmap ((str.str()).c_str(),NULL);
        if(!StandImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);
}
    }
     
  for (int i = 1; i < WalkFrames; i++)
   {
    stringstream str;
    str << "Final Battle\\" << name << "\\" << name << "Walk" << i << ".bmp"
    wcounter += 1;
    WalkImage[i] = load_bitmap ((str.str()).c_str(), NULL);
    if(!WalkImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);
}

 }


 
  for (int i = 1; i < AttackFrames; i++) { // note i put <= to in standframes
    stringstream str;
    str << "Final Battle\\" << name << "\\" << name << "Attack" << i <<".bmp";
    acounter += 1;
    AttackImage [i] = load_bitmap ((str.str()).c_str(),NULL);
        if(!AttackImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);
}
    }

    for (int i = 1; i < SAFrames; i++) { // note i put <= to in standframes
    stringstream str;
    str << "Final Battle\\" << name << "\\" << name << "SecondAttack" << i <<".bmp";
    sacounter += 1;
    SAImage [i] = load_bitmap ((str.str()).c_str(),NULL);
        if(!SAImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);
}
    }

    // for foing a  weak attack
    for (int i = 1; i < WeakFrames; i++) { // note i put <= to in standframes
    stringstream str;
    str << "Final Battle\\" << name << "\\" << name << "Weak" << i <<".bmp";
    wecounter += 1;
    WeakImage [i] = load_bitmap ((str.str()).c_str(),NULL);
        if(!WeakImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);
}
    }

  Transport = load_bitmap ("Final Battle\\Cloud\\CloudTransport.bmp",NULL);
     dir = -90;
     //buffer = create_bitmap (640,480);
     //y = 360;
     attacking = false;
     transporty = 400;
     atknum = 0;
     secondactivate = false;
     Aatkx = 0;
     weakend = false;
  }// end of Player::Player


void Player::think (BITMAP *Buffer) {
  Player::Controls ();
  Player::move (Name , dir , Buffer);
  Player::attack (Name , Buffer );
  Player::secondattack (Name , Buffer );
  Player::weak (Name , Buffer);
  }

// used to control the player
void Player::Controls () {
  if (player1 == false) {
    if (key [KEY_RIGHT] && attacking == false)
       dir = 90;
    else if (key [KEY_LEFT] && attacking == false)
      dir = 180;
    else if (key [KEY_DOWN] && attacking == false)
      atknum = 1;
    else if (key [KEY_SLASH] && attacking == false)
      atknum = 2;
     
    if (!key [KEY_RIGHT] && dir == 90  ) {
      dir = -90;
      walkcounter = 0; }
    else if (dir == 180 && !key [KEY_LEFT]) {
      dir = -180;
      walkcounter = 0; }
    }

  else if (player1 == true) {
    if (key [KEY_D] && attacking == false)
      dir = 90;
    else if (key [KEY_A] && attacking == false)
      dir = 180;
    else if (key [KEY_S] && attacking == false)
      atknum = 1;
    else if (key [KEY_Q] && attacking == false)
      atknum = 2;

    if (!key [KEY_D] && dir == 90) {
      dir = -90;
      walkcounter = 0;}
    else if (!key[KEY_A] && dir == 180) {
      dir = -180;
      walkcounter = 0;}
      }
  }

// for player movement

void Player::move (string name , int dir, BITMAP *buffer) {
  if (dir == 90 && attacking == false ){
walkcounter += 1;
if (name == "Cloud") {
  x += 15;
  draw_sprite (buffer, Transport , x , transporty);
  }
else {
x += 5;

if (walkcounter >= wcounter / 2 )
walkcounter = 1;
draw_sprite (buffer, WalkImage [walkcounter] , x , y );
  }
 } // end of dir = 90

  if (dir == 180 && attacking == false) {
    if (walkcounter < wcounter / 2 )
      walkcounter = wcounter / 2 + 1;
    walkcounter += 1;
    if (name == "Cloud") {
      x -= 15;
      draw_sprite (buffer, Transport , x ,transporty );
      }
    else {
      x -= 5;
   

  if (walkcounter >= wcounter )
    walkcounter = wcounter / 2 + 2;
  draw_sprite (buffer, WalkImage [walkcounter], x , y );
      }
    }

  if (dir == -90 && attacking == false)
    draw_sprite (buffer, StandImage [1], x, y);
  if (dir == -180 && attacking == false)
    draw_sprite (buffer, StandImage [2], x ,y);
} // end of Player Move
 

// for weak attacks

void Player::attack (string name , BITMAP *buffer) {
  if (dir == 90 && atknum == 1 && secondactivate == false || dir == -90 && atknum == 1 &&
    secondactivate == false) {
    attacking = true;
    atkcounter += 1;
    x += 5;
    if (name == "Cloud") {
      y -= 6;
      rest (10);
}

    draw_sprite (buffer, AttackImage [atkcounter] , x , y );
    if (atkcounter >= acounter / 2) {
      if (name == "Cloud")
        y += 6 * atkcounter;
      atkcounter = 0;
      atknum = 0;
      attacking = false;
      }
    } // end of dir = 90

  else if (dir == 180 && atknum == 1 && secondactivate == false || dir == -180 && atknum == 1 &&
    secondactivate == false) {
    attacking = true;
    if (atkcounter < acounter / 2 )
      atkcounter = acounter / 2;
    atkcounter += 1;
    x -= 5;
    if (name == "Cloud") {
      y -= 6;
      rest (10);
      }

    draw_sprite (buffer, AttackImage [atkcounter] , x , y );
    if (atkcounter >= acounter) {
      if (name == "Cloud")
        y += 6 * atkcounter / 2;
      atkcounter = 0;
      atknum = 0;
      attacking = false;
      }

    } // end of dir = 180

  } // end of player attack

void Player::secondattack (string name , BITMAP *buffer) {
  if (dir == 90 && atkcounter >= acounter / 2 - 1 || dir == - 90 && atkcounter >= acounter / 2 -1 ) {
    if (key[KEY_S]) {
      secondactivate = true;}
   
    if (secondactivate == true) {
    rest (20);
    secondcounter += 1;
    draw_sprite (buffer, SAImage [secondcounter] , x , y );
    if (secondcounter >= sacounter / 2) {
      if (name == "Cloud")
      y += 6 * atkcounter;
      atkcounter = 0;
      secondcounter = 0;
      atknum = 0;
      attacking = false;
      secondactivate = false;
     
      } // when the attack is finished

      } // if secondactivate is true
    } // if dir = 90;

  else if (dir == 180 && atkcounter >= acounter - 1 || dir == - 180 && atkcounter >= acounter -1 ) {
        if (key[KEY_S]) {
      secondactivate = true;}
   
    if (secondactivate == true) {
    if (secondcounter < sacounter / 2 )
      secondcounter = sacounter / 2;
    rest (20);
    secondcounter += 1;
    draw_sprite (buffer, SAImage [secondcounter] , x ,y );
    if (secondcounter >= sacounter) {
      if (name == "Cloud")
      y += 6 * atkcounter / 2;
      atkcounter = 0;
      secondcounter = 0;
      atknum = 0;
      attacking = false;
      secondactivate = false;
     
      } // when the attack is finished

      } // if secondactivate is true
    } // if dir = 180;
   

  } // end of player second attack

void Player::weak (string name , BITMAP *buffer) {
  if (dir == 90 && atknum == 2 || dir == -90 && atknum == 2 ) {
    attacking = true;
    if (weakend == false) {
    weakcounter += 1;
    if (name == "Cloud") {
      rest (20);
      y = 360; }
    draw_sprite (buffer, WeakImage [weakcounter] , x , y); }
   

    if (name != "Axel" && weakcounter >= wecounter / 2 ) {
      if (name == "Cloud") {
        rest (70);
        y = 350;}
      weakcounter = 0;
      atknum = 0;
      attacking = false;
      }


    if (name == "Axel" && weakcounter >= 3) {
      weakend = true;
      Aatkx += 25;
      draw_sprite (buffer, WeakImage [9] , x + Aatkx , y );
      draw_sprite (buffer, WeakImage [4] , x , y );
      if (x + Aatkx >= 640) {
        Aatkx = 0;
        weakcounter = 0;
        atknum = 0;
        attacking = false;
        weakend = false;
        } // axel weak finished
      } // if player is Axel

    } // dir == 90

  if (dir == 180 && atknum == 2 || dir == -180 && atknum == 2 ) {
    attacking = true;
    if (weakend == false) {
    if (weakcounter < wecounter / 2 ) {
      weakcounter = wecounter / 2;
      if (name == "Cloud")
        weakcounter = wecounter / 2 + 1;
      }
    weakcounter += 1;
    if (name == "Cloud") {
      rest (20);
      y = 360; }
    draw_sprite (buffer, WeakImage [weakcounter] , x , y); }
   

    if (name != "Axel" && weakcounter >= wecounter) {
      if (name == "Cloud") {
        rest (70);
        y = 350; }
      weakcounter = 0;
      atknum = 0;
      attacking = false;
      }


    if (name == "Axel" && weakcounter >= 7) {
      weakend = true;
      Aatkx += 25;
      draw_sprite (buffer, WeakImage [9] , x - Aatkx , y );
      draw_sprite (buffer, WeakImage [8] , x , y );
      if (x - Aatkx <= 0) {
        Aatkx = 0;
        weakcounter = 0;
        atknum = 0;
        attacking = false;
        weakend = false;
        } // axel weak finished
      } // if player is Axel

    }
   
  } // end of player weak
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sat Sep 11, 2010 2:16 pm   Post subject: RE:Collision Class 2 player game

I don't understand. What do you mean by collision class?

I think you mean how to stop two objects from going through each other? If so, use rectangular collision detection. A quick google search found this tutorial.
DrummaBoyFB




PostPosted: Sat Sep 11, 2010 2:39 pm   Post subject: Re: Collision Class 2 player game

I know how to do collision. The problem is that I don't know how to set the x and y variables apart.
It's hard to explain so I'll make it easier

How would I code it so that when Axel and Cloud (my two players) get close it shows a message on the screen
and then when they get farther apart it doesnt show the message anymore.

Example : If (Cloudx - Axelx < 10)
allegro_message ("They've collided");
DrummaBoyFB




PostPosted: Sat Sep 11, 2010 2:44 pm   Post subject: Re: Collision Class 2 player game

That site you gave me helped me a bit but I don't really get it. I'm a beginner at game programming for C++
so I don't want a complicated method , I just want it so that when my players come within a certain distance within each other something happens.
I was thinking and I suppose I could use pointers so I'll look into that.... I know some of what I'm saying might not make sense but please try and bare with me
DrummaBoyFB




PostPosted: Sat Sep 11, 2010 2:53 pm   Post subject: Re: Collision Class 2 player game

OHHHH I just figured out the problem lol , wow I'm such an idiot .
Sorry for wasting your time
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: