I was going to just use newton for collsion but since i am just learning c++ i decided it would better to get the language down first so i converted kenspun code to c++ for dark sdk.
Collsion.cpp
#include "stdafx.h"
#include "Collision.h"
void Collision( float colx, float coly, float colz, int Level )
{
for ( x=0; x < checks-1; x++ )
{
chx = dbNewXValue( colx,x*angwidth,100 );
chz = dbNewZValue( colz,x*angwidth,100 );
ang[x][1] = dbIntersectObject( Level,colx,2,colz,chx,coly,chz );
if ( ang[x][1] == 0 )
{
ang[x][1] = 999999;
}
ang[x][2] = x*angwidth;
sort[x][1] = ang[x][1];
sort[x][2] = ang[x][2];
}
// rem sort the array to find the closest object and it's degrees
for ( x=0; x < checks-2; x++ )
{
for ( y=x+1; y < checks-1; y++ )
{
if ( ang[x][1]>ang[y][1] )
{
temp = ang[x][1];
temptwo = ang[x][2];
ang[x][1] = ang[y][1];
ang[x][2] = ang[y][2];
ang[y][1] = temp;
ang[y][2] = temptwo;
}
} // end Y
} // End X
//print "Player Distance to Wall=",ang#(0,1)
//`find +-90 degrees from the closest one for when you walk into a corner so it doesn't shake
prev = dbWrapValue(ang[0][2]-90)/angwidth;
nxt = dbWrapValue(ang[0][2]+90)/angwidth;
newd = radius-ang[0][1];
newa = dbWrapValue(ang[0][2]-180);
newd1 = radius - sort[prev][1];
newa1 = dbWrapValue(sort[prev][2]-180);
newd2 = radius-sort[nxt][1];
newa2 = dbWrapValue(sort[nxt][2]-180);
//`if you are less than radius from a wall, push you out to 20 from it
if ( ang[0][1] == radius && ang[0][1] == 0.0 )
{
repx = dbNewXValue(colx,newa,newd);
repz = dbNewZValue(colz,newa,newd);
dbPositionObject ( Player[1].Obj,repx,coly,repz );
}
//``````````````````````````````````````````````````
colx = dbObjectPositionX(Player[1].Obj);
colz = dbObjectPositionZ(Player[1].Obj);
if ( sort[prev][1] == radius && sort[prev][1] == 0.0 )
{
repx1 = dbNewXValue(colx,newa1,newd1);
repz1 = dbNewZValue(colz,newa1,newd1);
dbPositionObject ( Player[1].Obj,repx1,coly,repz1 );
}
colx = dbObjectPositionX (Player[1].Obj);
colz = dbObjectPositionZ (Player[1].Obj);
//`this is if you are coming into a corner, this pushes you sideways
if ( sort[nxt][1] == radius && sort[nxt][1] == 0.0 )
{
repx2 = dbNewXValue(colx,newa2,newd2);
repz2 = dbNewZValue(colz,newa2,newd2);
dbPositionObject ( Player[1].Obj, repx2, coly, repz2 );
}
// Gravity.
oldht = coly;
// rem Finds The Distance From The Camera To The Floor
sub = dbIntersectObject(Level,colx,oldht,colz,colx,oldht-cameraheight,colz);
//rem If A Number Smaller Than The Specified Floor Height Is Returned Then...
if ( sub < cameraheight )
{
cy = (oldht + cameraheight) - sub;
//rem reposition camera
dbPositionObject ( Player[1].Obj,colx,cy,colz );
}
// rem If The Camera Is Not "Touching" The Floor (i.e. Floor Height + Specified Camera Heght) Then
if ( sub == 0 )
{
//rem reset camera position
dbPositionObject ( Player[1].Obj,colx,oldht,colz );
}
}
Collision.h
#ifndef __INTRO_H__
#define __INTRO_H__
// was going to use newton for physics but i decied i need to learn c++ better
// befor i start messing with physics.
#include "Player.h"
void Collision( float colx, float coly, float colz, int Level );
float radius = 20;
int checks = 4;
int angwidth = 360/checks;
float ang[3][2];
float sort[3][2];
float chx;
float chz;
float temp;
float temptwo;
int prev;
int nxt;
float newd;
float newa;
float newd1;
float newa1;
float newd2;
float newa2;
float repx;
float repz;
float repx1;
float repz1;
float repx2;
float repz2;
float oldht;
float cameraheight =20.f;
float sub;
float cy;
int x;
int y;
#endif