Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: two questions: "collision-stop-detect" and "random move"  (Read 5277 times)

0 Members and 1 Guest are viewing this topic.

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
two questions: "collision-stop-detect" and "random move"
« on: January 06, 2014, 10:31:39 am »
Hello!!

Im working on new projects and there is two things that I dont understand.

Question 1:
I know how I can make a Collision with this Code:
object.getGlobalBounds().intersects( player.getGlobalBounds()....

I know, how I can "destroy" the object or the player, set a other position for the players etc.

But.. if I want to make a 2D-RPG game, how should the Code looks like, if I want that the Player "stops" to the object? Should I work with booleans? I never implented this and I have no idea how I can do this.
I think, I would do this (pseudo code):

if (object.getglobalBounds intersects player globalbounds
   &&pressRight=true)
{
   Walkspeed = 0;
}
else if(object.getglobalBounds intersects player globalbounds
   &&pressRight=true)
{
   Walkspeed = 0;
} //(...)
else
{
   Walkspeed = X
}

or this:

if(...) &&right=true
{
 move(x-1,y)
}

But I know its stupid and wrong, and I dont know if this stupid code is working.
Can you tell me, how you do this? How you create a Code that the player "stops" at the objects?

Question 2:

This is a bit more complex (maybe..): I want to give every Sprite his own moves.

For example, please have a look at this pseudo code:

timer >= 100
{

randomz = rand() % 5 + 1;

if(randomz = 1)
  randomx = 5, randomy  = 3;
if(randomz = 2)
  randomx = -2, randomy = -5;
(...)

Create Sprite process

sprite*move(randomx, randomy)
timer = 0
}

The problem is: I dont know, if every sprite get his "own" move, or its a "global move" like: movement is changing every time for all sprites.

Kursukia :)

Sorry for my broken english!

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: two questions: "collision-stop-detect" and "random move"
« Reply #1 on: January 06, 2014, 11:43:22 am »
The basic idea is to "simulate" the movement e.g. calculate the position of the next frame and only move if it is valid.

Kind Regards

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: two questions: "collision-stop-detect" and "random move"
« Reply #2 on: January 06, 2014, 04:44:56 pm »
The basic idea is to "simulate" the movement e.g. calculate the position of the next frame and only move if it is valid.

Kind Regards

Hey Raincode,

just like this?

if (!object.getglobalBounds intersects player globalbounds)
{
    if(keyboard::isPressed(......)

}
else
{
// nothing
}

kursukia

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: two questions: "collision-stop-detect" and "random move"
« Reply #3 on: January 06, 2014, 05:39:12 pm »
Well basically, but A you are using the current bounding box, but you want to use the bounding box for the next frame... Add the velocity * frameTime to the topLeft corner of the hit box (there might be better ways, I don't know)

Kind Regards

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: two questions: "collision-stop-detect" and "random move"
« Reply #4 on: January 06, 2014, 08:45:39 pm »
Its it fine question to ask, i handled my movement and collision this way:

I did the movement like this.

Pseudo code
Code: [Select]
bool inputW, inputS, inputA, inputD;//This user uses to issue commands to the unit

//At unit code for movement
void LoopUnitsLogic()
{
int destinationX = unit.getPosX();
int destinationY = unit.getPosY();

if(inputW)//Check if the user issued for this unit to move up
{
destinationY -= unit.getMovementRate();//Notice the - it is because we are moving upwards
}
else if(inputS)//It is simple finish it!

if( Check_If_Position_Walid(destinationX, destinationY) )
{
unit.moveTo(destinationX, destinationY);
}
}

bool Check_If_Position_Walid(int posX, int posY)
{
//In here i was a cheeky little bas... and i was smart enough to grab the tiles my unit was standing on and check only those so i didn't have to look true the whole map because it was big
}
Do not use real time events, the sprites will look like they are having seizures!
Quote
Sometimes, people try to react to KeyPressed events directly to implement smooth movement. Doing so will not produce the expected effect, because when you hold a key you only get a few events (remember, the repeat delay). To achieve smooth movement with events, you must use a boolean that you set on KeyPressed and clear on KeyReleased; you can then move (independently of events) as long as the boolean is set.
The other (easier) solution to produce smooth movement is to use real-time keyboard input with sf::Keyboard (see the dedicated tutorial).
« Last Edit: January 06, 2014, 08:48:47 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

kursukia

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: two questions: "collision-stop-detect" and "random move"
« Reply #5 on: January 07, 2014, 10:17:17 pm »
Hello :)

Thank you. I found the solution myself and its a bit the same like yours.

pseudo code:

move A,
move D,
move W,
move S

if collision with box // <- globalbounds
setPosition // <- just "setPosition"
else
//nothing// <- nothing :P
 


so for over classes I going to use a simply void:

collisionCheck(player) //or something similar, dont know

and I going to put ALL boxes into one cpp class file. For every sprite to check the same code.

it was really really simply. Thank you very much. :)

Edit:

To check all objects, my code is not really effective I think. I didnt try out.

But I would do this method:

Pseudo code main.cpp / game.cpp:

treeObjectSprite, houseObjectSprite, stoneObjectSprite, (...)

objectList, objectIterator

treeObjectSprite pushback objectList
houseObjectSprite pushback objectList
stoneObjectSprite pushback objectList

collision.cpp
for objectList size ++ something
{
   if player globalbounds objectList[iterator]
   {
      setPosition player
   }
   else
   {
   // nothing
   }
}

But.. would do work this? Is there a better method to do this? For example, I have at "level 1" over 25 objects.

This method would be really effective, if it works. I can try this out in the evening (its 10 am here now) to test if it would work this.

kursukia
« Last Edit: January 08, 2014, 10:23:21 am by kursukia »

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: two questions: "collision-stop-detect" and "random move"
« Reply #6 on: January 08, 2014, 03:11:03 pm »
please, either write your "pseudocode" in rather plain english, so people can understand what you mean, or write real code. At the moment, I can't really understand your code examples

Kind Regards

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: two questions: "collision-stop-detect" and "random move"
« Reply #7 on: January 08, 2014, 10:16:52 pm »
For me i hold everything in one vector.
To tell this simplest as possible:
I have a class that is supposed to hold data for unit for example
Code: [Select]
//The base that each unit i want to move, display needs to have
class UnitBase
{
public:
int VectorPosition;
bool inUse;
//Other unit related position, position sprite etc
};

//Class holder
Class EntityManager
{
public:
void AddEntity(UnitBase &unit);
void RemoveEntity(UnitBase &unit);
void MoveEntity(UnitBase &unit);
std::vector<Entity> UnitList;
};
void EntityManager::AddEntity(UnitBase &unit)
{
if(unit.inUse)
    return false;//Unit alredy exists, send error
else
//Create unit
}
void EntityManager::MoveEntity(BaseUnit &unit)
{
vertex[unit.VertexPosition].setPosition//...
}
//The remove is following same logic
The poent is to be leak free, so if in some case i somehow try to create unit two times it wont actually duplicate it and make it draw 2 sprites.
Its really base and as simple as i could explain, i strongly suggest for you to experiment that is the most fun thing to do  ;D.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0