Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Dark Basic COLLISION HELP! Please

Author
Message
Mattguse
11
Years of Service
User Offline
Joined: 9th Feb 2014
Location:
Posted: 9th Feb 2014 22:48
I am making a basic 3d game to start myself off, and I am making the wall which need collision boxes. I have it coded to have the sphere to change to red when hit is detected but I can't get the object to move back to the position before a hit was detected.
Heres my code:

Sync On
Sync Rate 60
Hide mouse
Backdrop on
Color Backdrop 0
Set camera range 1,5000
set global collision on
Fog on
Fog distance 4000
Fog color RGB(128,128,128)
Color Backdrop RGB(0,256,0)
Set Cursor 0,0
Set Display mode 800,600,32

REM Load images
LOAD IMAGE "C:\Users\matt\Desktop\slate.jpg", 1, 1,1

Rem make matrix
Make matrix 1,5000,5000,30,30
PREPARE MATRIX TEXTURE 1, 1, 1, 1

REM START POSITION
X# = 200
Z# = 200
REM MAKE OBJECT
Make Object sphere 1,100
Position Object 1, 200,0,200
Color Object 1, rgb(0,200,0)
SET OBJECT COLLISION ON 1
SET OBJECT COLLISION TO Spheres 1
rem make roof, wall
Make Object Plane 2,5000,5000,1
Rotate Object 2,270,0,0
Position Object 2,2500,300,2500

Make Object Plane 3,5000,300,300
Rotate Object 3,0,0,0
Position Object 3,2500,150,0

Make Object Plane 4,5000,300,300
Rotate Object 4,0,90,0
Position Object 4,5000,150,2500

Make Object Plane 5,5000,300,300
Rotate Object 5,0,0,0
Position Object 5,2500,150,5000

Make Object Plane 6,5000,300,300
Rotate Object 6,0,90,0
Position Object 6,0,150,2500
REM INNER WALLS
Make Object Box 7,4700,300,25
Rotate Object 7,0,0,0
Position Object 7,2650,150,2500
MAKE OBJECT COLLISION BOX 7,-2350,-150,-12.5,2350,150,12.5
SET OBJECT COLLISION ON 7
SET OBJECT COLLISION TO Boxes 7
REM MAKE LIGHTS
SET AMBIENT LIGHT 0
Make Light 1
color light 1,512,512,255
set light range 1,500


Rem Main loop
Do

set camera to follow X#,Y#+50,Z#,0,0,75,1,0
set camera to object orientation 1

OldCamAngleY# = CameraAngleY#
OldCamAngleX# = CameraAngleX#
CameraAngleY# = WrapValue(CameraAngleY#+MousemoveX()*0.2)
CameraAngleX# = WrapValue(CameraAngleX#+MousemoveY()*0.2)

Rem Control input for camera


If keystate(17)=1
XTest# = Newxvalue(X#,CameraAngleY#,10)
ZTest# = Newzvalue(Z#,CameraAngleY#,10)
If XTest#>40 and XTest#<4960 and ZTest#>40 and ZTest#<4960
X#=XTest#
Z#=ZTest#
Endif
Endif

If keystate(31)=1
XTest# = Newxvalue(X#,Wrapvalue(CameraAngleY#-180),10)
ZTest# = Newzvalue(Z#,Wrapvalue(CameraAngleY#-180),10)
If XTest#>40 and XTest#<4960 and ZTest#>40 and ZTest#<4960
X#=XTest#
Z#=ZTest#
Endif
Endif

Yrotate camera CurveAngle(CameraAngleY#,OldCamAngleY#,24)
Xrotate camera CurveAngle(CameraAngleX#,OldCamAngleX#,24)

Y# = Get ground height(1,X#,Z#)
Position Object 1,X#,Y#+1,Z#

ROTATE LIGHT 1,CameraAngleX#,CameraAngleY#,Z#
Set point LIGHT 1,X#,Y#+50,Z#




Rem Collisions
If OBJECT COLLISION(1,7)>0

Color Object 1, rgb(200,0,0)
endif


REM QUIT GAME
if escapekey() = 1
EXIT
ENDIF

Rem Refresh Screen
Sync
Loop
Blobby 101
18
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 10th Feb 2014 10:30
For future reference, try to start indenting your code - it makes it much easier to read for other people if/when you need help! (and you can put code in code tags on this forum as well, there's a button in the post form )

About your actual question though, one way of doing this is to record your position at the start of every loop, before you apply movement:


Then in your collision checks, reset the position to the old positions if there's a collision. It'll only give you simple "sticky" collision, but it's very easy to implement.

If you want proper sliding collision, I'd advise using Sparky's collision plugin instead of trying to code it yourself from scratch - it'll save you a lot of headaches!

Mattguse
11
Years of Service
User Offline
Joined: 9th Feb 2014
Location:
Posted: 10th Feb 2014 17:39
Thanks for your help! After recording the old position when I run into my wall it will set me back to the last position, but after that I can still go through the wall.
I am going to look into Sparky's collision plugin thanks.
Mattguse
11
Years of Service
User Offline
Joined: 9th Feb 2014
Location:
Posted: 10th Feb 2014 17:40
Thanks for your help! After recording the old position when I run into my wall it will set me back to the last position, but after that I can still go through the wall.
I am going to look into Sparky's collision plugin thanks.
timer help
11
Years of Service
User Offline
Joined: 14th Jan 2014
Location:
Posted: 10th Feb 2014 19:21
please use code snippets
timer help
11
Years of Service
User Offline
Joined: 14th Jan 2014
Location:
Posted: 10th Feb 2014 19:21
please use code snippets

Login to post a reply

Server time is: 2025-05-15 10:24:39
Your offset time is: 2025-05-15 10:24:39