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.

Newcomers DBPro Corner / Help needed with making players jump in a .X world

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 26th Feb 2007 16:58
I'm starting work on an RPG using DarkBasic Classic, but I'm having a lot of trouble writing some code that controls the character's movements and prevents him flying through the walls of the world.

I'd managed to get the basic movements to work (like forwards and backwards)but It all goes wrong when I try to write code that lets the player jump.

This is the code I've been using:


if spacekey()=1 and playergrav#=0.0 then playergrav#=2.0

rem increase the y#
playergrav#=playergrav#-0.1
y#=y#+playergrav#

rem Update character
position object 1,x#,y#,z#
yrotate object 1,a#

rem if the object hits anything, decrease the x#, y# and z# accordingly
if object collision(1,0)>0
playergrav#=0.0
x#=oldx#
y#=oldy#
z#=oldz#
endif


The code as it stands won't allow the player to move at all - the y-coordinate will always decrease to zero, and so register a collision with the world. The x#, y# and z# are then reset to what they used to be at the start of the main programming loop, and so they'll never change.

However, it all works brilliantly if I rem out the 'x#=oldx#' and the 'z#=oldz#' in the object collision if-statement. Unfortunately, that means the player can fly right through the walls of the world, and he's not supposed to be a ghost...

Is there any way that I can make the player jump and not disappear through a 'solid' wall?

Something else I've noticed with the code is that the player can also latch onto the ceiling if it's low enough and keep moving through the world, as long as the space bar stays pressed. Is there a way around this as well?
Hammaman
20
Years of Service
User Offline
Joined: 11th Feb 2004
Location:
Posted: 28th Feb 2007 13:41 Edited at: 28th Feb 2007 13:42
Given the lack of response so far, I thought that I'd have a go at answering this ...

The issues you are experiencing relate to the way you are testing for collisions and the fact that your world is just one object in the game. The response to collisions with the floor are different that that with the walls, but the collision detection cannot differentiate between these two things.

So one solution is to rebuild your world splitting the walls out separately into another object (and possibly the ceiling while you're at it!).

Alternatively, I think there is a "rough and ready" solution by playing with the collision detection as follows:

At the moment, you are testing for movement in three directions (x,y and z) at the same time. But the as the y-collision is with the floor after jumping, my suggestion is to test for this separately, i.e.



I think this code will work if you are controlling the camera synchronisation with the sync command (otherwise, you will see the object move in two steps).

Regarding the ceiling problem, the issue here is that the ceiling is part of the object and so when you collide with it, the code resets the playergrav to zero, allowing you to jump again by pressing the spacebar. I can't think of an easy solution to fix this using the existing code - all I can suggest is to split the ceiling out into another object. Again, the fundamental point is that your response to a ceiling collision is different to a wall.

Hope this helps.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 28th Feb 2007 14:45
Thanks for the advice, I'll give that code snippet a go later tonight.

However, I'm not sure if I can split the model down into separate pieces like walls, floor and ceiling - you see, I use Maplet to build my levels (it is for BlitzBasic, I know, but it's the easiest one I've worked with) and it can't create flat polygons.

Do you think it would work if I used DarkBasic to make an invisible plain, position it (say) 0.1 below the ceiling and then tested that for object collision?
Hammaman
20
Years of Service
User Offline
Joined: 11th Feb 2004
Location:
Posted: 28th Feb 2007 17:19
Yes, you could introduce a hidden ceiling object to test against.


But if I understand you correctly, if you're going to position it just below the ceiling, doesn't this mean that you know where the ceiling is (i.e. it's y co-ordinate) and so you can amend your collision code to reflect when you hit the ceiling?

I'm thinking of adding code along the lines of testing if y# > ceiling_y# then (react to ceiling hit).
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 1st Mar 2007 09:56
I hadn't thought of that...

The problem is that with the levels I'm building, the 'ceiling' isn't always at the same y#. The world I was using in my code was a spiral staircase, where the player could move underneath some stairs, jump, and hit the stair above. What I was thinking I'd have to do was make several hidden polygons - one underneath each stair - and test for object collision with them.

I know it's cumbersome, but it's all I can think of for stopping the player hitting the ceiling. I've tried adjusting the code to use a variable 'jump':

If spacekey()=1 and playergrav#=0.0 and jump=0 then playergrav#=2.0 and jump=1

but I can't figure out how or where to reset it afterwards. If I wait for a y-coordinate object collision, the player will be able to stick to the ceiling.

Any idea how to get round this?
Hammaman
20
Years of Service
User Offline
Joined: 11th Feb 2004
Location:
Posted: 2nd Mar 2007 13:38
I see your problem ...

However, I think I have a solution - I think it involves looking at playergrav#.

If playergrav# > 0, then the player is moving upwards (i.e. still jumping) and if playergrav# < 0, then the player is falling towards the ground.

So if the player hits the ceiling, then he/she must be moving upwards and so playergrav will be greater than zero. So I guess you just have to set playergrav to be -0.1 and let the player fall back to earth.

I hope this points you in the right direction!

By the way, I think the jump variable you introduced is a good idea - you will need to reset it to 0 when the player collides with the object and playergrav < 0.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 5th Mar 2007 09:48
Sorry for the delay in replying, but I've not been online over the weekend. Tried your code and IT WORKS!!!!! YOU'RE AN ABSOLUTE GENIUS!!!!!!!!!!!!!!!!!!!

Now all I need to do is figure out how to get the player to negotiate ramps...

Login to post a reply

Server time is: 2024-09-25 19:12:27
Your offset time is: 2024-09-25 19:12:27