View Single Post
  #13  
Old 06-05-2012, 06:05 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
I think something like this would be a more appropriate way to check for collisions, albeit there's more calculation going on:

PHP Code:
for (temp.pplayers) {

  
// Determine width/height of player
  
temp.pw 2;
  
temp.ph 2;
  
// Find center of player
  
temp.px p..5 pw/2;
  
temp.py p.1  ph/2;

  
temp.tw temp.th 2;
  
temp.nx this.x;
  
temp.ny this.y;
  
// If NPC is not a showcharactor, try to find center
  
if (this.ani.name == null) {
    
temp.tw this.width;
    
temp.th this.height;
    
temp.nx this.tw/2;
    
temp.ny this.ty/2;
  } else {
  
// If NPC is a showcharacter, find proper center of character
    
temp.tw 2;
    
temp.th 2;
    
temp.nx this..5 tw/2;
    
temp.ny this.1  ty/2;
  }

  
// Check axis distance between NPC and player
  
temp.dx abs(px nx);
  
temp.dy abs(py ny);

  
// If distance on both axes are less than the width and height
  // of the NPC and player combined, by half(because you're 
  // checking from the center of the entities), then a collision
  // has occurred.
  
if (dx < (pw nw)/&& dy < (ph nh)/2) {
    
// Collision!
  
}


The problem with the other method is you're relying on a single pixel collision within a box, instead of looking for the fact that two box entities are collision(checking the center of the player entering the entire size of the NPC isn't considering the fact the player has a size as well).

However I threw this up in notepad and didn't test it myself. Your mileage may vary.
Reply With Quote