Pages: [1]
  Print  
Author Topic: Fancy Collision Script  (Read 1466 times)
Offline (Male) RetroX
Posted on: November 16, 2008, 07:00:04 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
This is a script that will move the object into a free position in the best way possible.  Simple, yet effective.
Code: [Select]
// Syntax: collision_free(max)
// Function: Moves at a maximum of "max" outside of solids.

// Define Vars
left=0;
right=0;
up=0;
down=0;

// Don't do anything if we're already collision free
if not place_free(x,y)
 {
 // Check to see which way is closest to get outside of a solid
 for (i=0;i<=argument0;i+=1;)
  {
  if place_free(x-i,y)
   {
   // Find out how far we can move left
   left=i;
   break;
   }
  }
 for (i=0;i<=argument0;i+=1;)
  {
  if place_free(x+i,y)
   {
   // Find out how far we can move right
   right=i;
   break;
   }
  }
 for (i=0;i<=argument0;i+=1;)
  {
  if place_free(x,y-1)
   {
   // Find out how far we can move up
   up=i;
   break;
   }
  }
 for (i=0;i<=argument0;i+=1;)
  {
  if place_free(x,y+1)
   {
   // Find out how far we can move down
   down=i;
   break;
   }
  }
 }

// If up is the highest, move up
if up>=left or up>=right or up>=down
 {
 y-=up;
 }

// If down is the highest, move down
if down>=left or down>=right or down>=up
 {
 y+=down;
 }

// If left is the highest, move left
if left>=right or left>=up or left>=down
 {
 x-=left;
 }

// If right is the highest, move right
if right>=left or left>=up or left>=down
 {
 x+=right;
 }
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) OniLink10
Reply #1 Posted on: November 19, 2008, 12:17:42 PM
Member
Joined: Jul 2008
Posts: 68

View Profile Email
Nice. Very useful, especially for Platformers and RPGs.
Logged
Offline (Male) RetroX
Reply #2 Posted on: November 19, 2008, 04:05:15 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Like how in some games you end up saving over the starting point of an object and ruin the entire game.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: [1]
  Print