About Store Forum Documentation Contact
Donations:
309$/mo



Post Reply 
Walls
Author Message
DreamsInHD Offline
Member

Post: #1
Walls
Hey,

I'm trying to make a small top down game just for practice, but I'm already having trouble, I obviously don't want to let the player go through walls, so I made functions to prevent that. This is where I'm stuck. The functions work, the player isn't able to go through this particular wall (wallLeft), but I think they're extremely inefficient.

bool checkMoveDown(Vec2 pos)
{
pos.y -= 0.5 * Time.d();
if(Cuts(pos, wallLeft))
{
return false;
}

return true;
}


I made one of these for every direction to check if the player will hit a wall if it makes a move. There must be a better, more efficient way to do this.

Thanks in advance. (beginner)
11-09-2015 09:49 PM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Silver Supporter

Post: #2
RE: Walls
It's not all bad.

A more generalized version could be something like:

PHP Code:
bool canMove(C Vec2 posC Vec2 dir) {
   
// pos contains the current position
   // dir contains the direction. For example when moving down, 
   // dir could be (0, -1 * speed * Time.d())

   
REPA(walls) {
      if(
Cuts(pos dirwalls[i])) return false;
   }
   return 
true;


Assuming walls is a container with your wall rectangles. As long as your example does not contain hundreds of walls there's no need to worry about performance.

Of course if you use physics these collisions could be handled automatically.
(This post was last modified: 11-12-2015 07:55 PM by yvanvds.)
11-12-2015 07:54 PM
Find all posts by this user Quote this message in a reply
DreamsInHD Offline
Member

Post: #3
RE: Walls
Thanks!
11-12-2015 08:00 PM
Find all posts by this user Quote this message in a reply
Post Reply