:: PlayerAdvance.org ::  

Précédent   :: PlayerAdvance.org :: > :: Forums Hébergés :: > OldSchool Library > Aide au développement

Aide au développement Aide sur l'utilisation d'OSLib

Publicité

Réponse
 
Outils de la discussion Modes d'affichage
Vieux 21/02/2009, 23h29   #1
pspmasterpro
Membre
 
Date d'inscription: 22/01/2009
Messages: 17
Par défaut [OSLIB] Platformer physics (Jumping and Gravity)

Hello I'm trying to create physics for my platformer (jumping and gravity). But when I press (X) the player just swaps to the vertical height. So lets say when I press X the player's Y cordinate will decrease by 64 so it will jump into the air 64 pixels. But it does it all at once, I want it to slowly assend into the air 1 pixel at a time like in a real platformer. So basicly I wanted the player to continously assend untill it assended 64 pixels. I've tried implimenting it like this:

Code:
{......}
void newGame()
{
 int RLimit = 250;
 int LLimit = 50;
 extern int canJump;
 oslCls();

 // Draw map and player
 oslDrawMap(mapCollision1);
 oslDrawImage(player);

 // Active gravity
 if(!collisionMgr(mapCollision1,player,0,0,0,1) && canJump == !1)
 {
  player->y += 1;
 }
 else if(collisionMgr(mapCollision1,player,0,0,0,1))
 {
  canJump = 1;
 }
 else if(!collisionMgr(mapCollision1,player,0,0,0,1))
 {
  canJump = 0;
 }

 // Character movement with camera and collision detection
 if(osl_keys->held.right && !collisionMgr(mapCollision1,player,0,1,0,0))
 {
  if (player->x >= RLimit)
  {
    map1->scrollX += 1;
    mapCollision1->scrollX += 1;
  }
  else
  {
    player->x += 1;
  }
 }

 if(osl_keys->held.left && !collisionMgr(mapCollision1,player,1,0,0,0))
 {
  if (player->x <= LLimit)
  {
    map1->scrollX -= 1;
    mapCollision1->scrollX -= 1;
  }
  else
  {
    player->x -= 1;
  }
 }

 if(osl_keys->pressed.cross && !collisionMgr(mapCollision1,player,0,0,1,0))
 {
  if(canJump == 1)
  {
   player->y -= 64;
   canJump = 0;
  }
 }
}
{......}
I tried many other methods that came to mind they didn't work. I also tried using a variable that will increase up to 64 and then apply that with the player->y but that doesn't work either.

I asked this question on another forum and they directed me here
http://www.tonypa.pri.ee/tbw/tut07.html

I understand it a bit but some parts are complex If someone has a solution or can direct me to a better tutorial , then that would be great
pspmasterpro est déconnecté   Réponse avec citation

Publicité

Vieux 22/02/2009, 15h03   #2
t4ils
Membre confirmé
 
Date d'inscription: 10/11/2005
Localisation: Morangis ( 91 )
Messages: 2 754
Voir les codes amis Wii
Par défaut

jumping is not something very difficult to add in a game
first, you have to include gravity. It will increase your Y speed, simulating a falling object
('sy' is your character Y speed)
Code PHP:
while(true){//game loop
    
player->sy+=1;//adding the gravity
    
testCollisions();//testing the collisions so that you don't pass through the ground
    
player->y+=player->sy;//setting new Y position

Basically, this just makes your character falling until it hits the ground
when you're on the ground, you can jump !
Code PHP:
while(true){//game loop
    
player->sy+=1;//adding the gravity        
    
testCollisions();//testing the collisions so that you don't pass through the ground

    
if(player->touchingGround && osl_keys->pressed.cross){//player on the ground and we press X : we can jump !
        
player->sy = -20;//the player now has a negative speed : he will jump !
    
}
    
player->y+=player->sy;//setting new Y position

Each loop will increase the Y speed (after a jump, or when the character falls)
- Y speed will start being negative : the character will "fly"
- each loop increases this speed : the character reduces its jump
- at some point, speed reaches 0 : character is at the highest point of its jump
- then it falls

basically, to make a character jumping, you just have to input a negative Y speed (whatever value does the trick. A higher negative one makes a higher jump) to your character and the gravity will do the rest
__________________
Un renard, en plus de voler, ca code !

t4ils est déconnecté   Réponse avec citation
Vieux 22/02/2009, 20h20   #3
pspmasterpro
Membre
 
Date d'inscription: 22/01/2009
Messages: 17
Par défaut

Thanks so I tried it like this:
Code PHP:
{......}

typedef struct
{
float sy;
}
player;
player.sy 0;

{......}

void newGame()
{
 
// adding the gravity
 
player.sy += 1;

 
// if player touches the ground
 
if(collisionMgr(mapCollision1,player,0,0,0,1))
 {
  
player.sy 0;
 }

 
// If (X) is pressed and player is on the ground
 
if(osl_keys->pressed.cross && collisionMgr(mapCollision1,player,0,0,0,1))
 {
  
player.sy = -20;
 }
 
player->+= player.sy;
}

{......} 
It does work my player does jump up and comes down
but it would get stuck in the tile when he'd come back down and if he is jumping and there is another tile above the player it would get stuck. I wasn't sure what would happen if my player comes back down so I set the speed to zero if thats not right please tell me.

EDIT: I think my collision system doesn't work well. What would be a good method of collision in this situation?

I used this method of collision from tutorial 10:
Code PHP:
short GetTile(OSL_MAP *m,int colint row)
{
 
u16 *map = (u16*)m->map;
 return 
map[row*m->mapSizeX col];
}

int collisionMgr(OSL_MAP *mOSL_IMAGE *Collisionint leftint rightint topint bottem)
{
 
#define TILE_NONSOLID 0x0000
 #define TILE_SOLID 0x0001

 
int tile;

 if(
left)
 {
  
tile GetTile(m,((m->scrollX Collision->1))/ m->tileX, (m->scrollY Collision->Collision->stretchY)/ m->tileY);
  if (!
tile)
  {
  
tile GetTile(m,((m->scrollX Collision->1))/ m->tileX, (m->scrollY Collision->y)/ m->tileY);
  }
 }

 if(
right)
 {
   
tile GetTile(m,((m->scrollX Collision->+Collision->stretchX 1))/ m->tileX, (m->scrollY Collision->Collision->stretchY)/ m->tileY);
   if (!
tile)
  {
  
tile GetTile(m,((m->scrollX Collision->+Collision->stretchX 1))/ m->tileX, (m->scrollY Collision->y)/ m->tileY);
  }
 }

 if(
top)
 {
  
tile GetTile(m,((m->scrollX Collision->+Collision->stretchX ))/ m->tileX, (m->scrollY Collision->1)/ m->tileY);
  if (!
tile)
  {
  
tile GetTile(m,((m->scrollX Collision->x))/ m->tileX, (m->scrollY Collision->1)/ m->tileY);
  }
 }

 if(
bottem)
 {
  
tile GetTile(m,((m->scrollX Collision->+Collision->stretchX ))/ m->tileX, (m->scrollY Collision->Collision->stretchY 2)/ m->tileY);
  if (!
tile)
  {
  
tile GetTile(m,((m->scrollX Collision->x))/ m->tileX, (m->scrollY Collision->Collision->stretchY 2)/ m->tileY);
  }
 }

 switch(
tile)
 {
    case 
TILE_NONSOLID:
    return 
0;
    break;

    case 
TILE_SOLID:
    return 
1;
    break;
 }

return 
0;

O.K. I need some kind of collision detection code that will not allow the player to fall though the tiles, but I'm not sure how to build it? Can anymore help me out?

EDIT: Alright, I think I figured it out how I can get the player out if it falls into the tile:

while(player-y is bellow the title)
{
player--;
}

As you can see I'm not completly sure about the while part I mean how would I get the current level of the tile using the collision system from tutorial 10?

Dernière modification par pspmasterpro ; 17/03/2009 à 17h23. Motif: Help
pspmasterpro est déconnecté   Réponse avec citation
Vieux 22/03/2009, 20h11   #4
pspmasterpro
Membre
 
Date d'inscription: 22/01/2009
Messages: 17
Par défaut

*bump
pspmasterpro est déconnecté   Réponse avec citation
Réponse

Liens sociaux

Publicité



Utilisateurs regardant la discussion actuelle : 1 (0 membre(s) et 1 invité(s))
 
Outils de la discussion
Modes d'affichage

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 06h26.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd. Tous droits réservés.
Version française #16 par l'association vBulletin francophone
Design par Ass-Itch, DJP et Dr.Vince