Character Collision by Mix Master MaXXX7th Jan 2005 9:52
|
---|
Summary This is a Character Collision code that stops your character from going through blocks and walls etc. Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com remstart The simplest, and most obvious, place to add the collision is the move_player function. This way the collision will automatically be done for each player. We know that the arena is 500 units square, and that the bottom corner is positioned at 0,0 and that the top corner is positioned 500,500. In the function we work out what the new position of the player is going to be and these are the values that can be used to check if the character is outside of the bounding square. After the new player values are calculated we have the variables xPos# and zPos#. These are what we will check for collision. remend if xPos#<0 then xPos#=0 remstart Simple eh? What this is doing is seeing if the player position is less than the minimum position of the arena. If we were to add this though you would see that the player gets stuck halfway through the wall. This is because the size of the player has not been taken into account. So 'if xPos#<0 then xPos#=0' becomes: remend if xPos#<5 then xPos#=5 if zPos#<5 then zPos#=5 remstart NOTE : I chose the value 5 because it looked good. This means that the player does not quite reach the wall but it is good enough. This is the value that will be used for all of the future player collision calculations. Below I have also added the code for the maximum position value. The maximum value was worked out by subtracting 5 from the width of the arena (500). This is what we end up with: remend `collision if xPos#<5 then xPos#=5 if zPos#<5 then zPos#=5 if xPos#>495 then xPos#=495 if zPos#>495 then zPos#=495 The move_player function with working collision now looks like this: `------------------------- `move the specified player `------------------------- function move_player(id,forward, backward, left, right) `---------------------------------- `get the required object properties `---------------------------------- xPos#=object position x(id) yPos#=object position y(id) zPos#=object position z(id) yAng#=object angle y(id) `----------------------- `sort out basic movement `----------------------- `apply forward movement if forward=1 xSpeed#(id)=xSpeed#(id)+newxvalue(0,yAng#,moveDist#(id)) zSpeed#(id)=zSpeed#(id)+newzvalue(0,yAng#,moveDist#(id)) endif `apply backward movement if backward=1 xSpeed#(id)=xSpeed#(id)+newxvalue(0,yAng#,moveDist#(id)*-1) zSpeed#(id)=zSpeed#(id)+newzvalue(0,yAng#,moveDist#(id)*-1) endif `apply left rotation if left=1 yrotate object id,wrapvalue(yAng#-4) endif `apply right rotation if right=1 yrotate object id,wrapvalue(yAng#+4) endif `-------------------------------------------------- `sort out friction and other physics related things `-------------------------------------------------- `work out value with friction xSpeed#(id)=xSpeed#(id)*friction#(id) zSpeed#(id)=zSpeed#(id)*friction#(id) `add gravity value ySpeed#(id)=ySpeed#(id)+gravity#(0) `work out the new position xPos#=xPos#+xSpeed#(id) zPos#=zPos#+zSpeed#(id) yPos#=yPos#-ySpeed#(id) `collision if xPos#>495 then xPos#=495 if zPos#>495 then zPos#=495 if xPos#<5 then XPos#=5 if zPos#<5 then zPos#=5 `work out the height of the character if yPos#<get ground height(1,xPos#,zPos#) ySpeed#(id)=ySpeed#(id)+(yPos#-get ground height(1,xPos#,zPos#)) yPos#=get ground height(1,xPos#,zPos#) endif `reposition the player object position object id,xPos#,yPos#,zPos# endfunction `Camera Collision remstart The camera collision can be done with exactly the same technique. The only difference is the name of the position variables and the amount that the camera has to stay from the side of the arena. So you can simply copy and paste the collision code. To make it work for the camera you have to swap xPos# for xCamPos# and zPos# for zCamPos#. The camera will not go through the walls anymore. I changed the minimum collision values to 1 and the maximum collision values to 499 but this is entirely up to you. Once completed the camera function looks something like this: remend `--------- `chase cam `--------- function chase_cam(id) `work out the angle of the object being chased yAng#=wrapvalue(object angle y(id)+180) `grab the cameras current position xPos#=object position x(id) yPos#=object position y(id) zPos#=object position z(id) `other variables camDist=15 camHeight=3 `work out new position xCamPos#=newxvalue(xPos#,yAng#,camDist) zCamPos#=newzvalue(zPos#,yAng#,camDist) `camera collision if xCamPos#>499 then xCamPos#=498 if zCamPos#>499 then zCamPos#=498 if xCamPos#<1 then XCamPos#=2 if zCamPos#<1 then zCamPos#=2 `work out camera height yCamPos#=get ground height(1,xCamPos#,zCamPos#)+camHeight if yCamPos#<yPos#+camHeight then yCamPos#=yPos#+camHeight `update camera position position camera xCamPos#,yCamPos#,zCamPos# point camera xPos#,yPos#+camHeight,zPos# endfunction remstart NOTE : The camera collision is added to the chase_cam function rather than the move_player function. remend |