A few simple questions regarding blocks

Hello, first and foremost, I’m dumb and an amateur, but willing to learn. I use Google extensively like the air I breathe and I love cabbages.

Now, being a beginner and having a burning desire to do stuff, I want to make something simple, but as all simple things are, they are hard for beginners, but I’m getting there.

I want to make something like Antarctic Adventure:

You have a set path, always moving forward, hopping here and there and reach the end goal. Rinse and repeat.
Nice and simple.

Questions:
Using logic editor (programming will come at it’s own pace, promise), I have the character going left and right, speeding forward and back, but how can I make my shape brake, but not go back? Instead of decelerating, making the force applied reach but not go less than 0?

Is this possible with the logic editor or will I have to go head first in programming?

Hello and welcome.

For braking, it might be best to try using states. The state can be seen by expanding the box with the + button to the left of the controller heading for the logic editor. You can add, subtract, set, and change (toggle?) states. In the first state, a constant forward velocity would be applied, but if you press back (as an example), it would set the state to be one with a slower forward velocity. The left and right movement could be on another state that’s added. If you want it to be a smoother interpolation, you might be able to do it with logic bricks, but it would be easier (to me, at least) to implement with Python.

I got this simple script slapped in my cube, using the API reference here. I understand all of it, but I can’t trigger the KX_INPUT_ACTIVE part.

import bge

cont = bge.logic.getCurrentController()
mainChar = cont.owner #This is our character

keyboard = bge.logic.keyboard

if keyboard.events[bge.events.WKEY] == bge.logic.KX_INPUT_NONE:
    print("Activate Forward!")
if keyboard.events[bge.events.WKEY] == bge.logic.KX_INPUT_ACTIVE:
    print("Lovin!")

Is there any addition I should implement in my code, or is it something other than coding I should be aware of?

Domo arigatô.

Here’s the final piece of code I wrote (and can understand fully, as it was written by hand!!!)


import bge

cont = bge.logic.getCurrentController()
mainChar = cont.owner
keyboard = bge.logic.keyboard

forward = keyboard.events[bge.events.WKEY]
brake = keyboard.events[bge.events.SKEY]
left = keyboard.events[bge.events.AKEY]
right = keyboard.events[bge.events.DKEY]
jump = keyboard.events[bge.events.SPACEKEY]

keypressed = bge.logic.KX_INPUT_ACTIVE

speed=mainChar.getVelocity()[1]

if keypressed in [forward]:
    if abs(speed)<5:
        mainChar.applyForce((0,5,0), True)
    else:
        mainChar.setLinearVelocity([0,5,0], True)

if keypressed in [brake]:
    if abs(speed)>0:
        mainChar.applyForce((0,-5,0), True)
    else:
        mainChar.setLinearVelocity([0,0,0], True)        

if keypressed in [left]:
    mainChar.worldPosition.x-=1

if keypressed in [right]:
    mainChar.worldPosition.x+=1
    
print(mainChar.getVelocity())

My cube with Dynamic Physics slides nicely, looses momentum if “W” key is depressed and brakes effectively if pressing “S”, not going back on it’s path. Yay me!

Now I just have to deal with the jump routine, because the height of the jump changes as the velocity changes also. The faster the cube, the lower the jump gets. I use A Keyboard Space Key Sensor with a Motion Actuator with a force of 50. At max speed (5), the cube barely moves up. I also added a Collision sensor to the mix. Me no like the puny jump.

The file is attached, if anyone cares to see it.

Thanks, guys.

see_cube_move.blend (445 KB)

I’m not sure but you might try getting the velocity of your cube using your getLinearVelocity, then using a little math to apply it to your jump force

always ------------and--------motion
Brake =0-----------/
----------------------
key (brake)---------and-------brake=1
(brake interval)if brake = or greater then or equal to 1 while less then or equal to 10-(property interval)-(brake interval)
(brake interval)--------and--------brake=brake+1
______________________---------Advance animation -stopping
_______________________--------play motion for each frame, or brake up into many separate forces/motions

if brake = 10 ----------------and-------assign property brake =0
so you press the brake key, it plays a 10 frame “slowing down” animation or translation or set of forces
and it will not be cumulative i.e. pressing brake repeatedly will not slow down more, because of the if brake =0 bit

If Player Collide property ground-----------And-------------Property JumpCheck=1
if jumpcheck =<10 while jumpcheck >=1 add 1 to jumpcheck //length of time between jumps, or walljumps etc
if jumpcheck=1 -----------------and--------------------motion Force Z or whatever you are using
keypress jump-------------------/
if jumpcheck = 10--------------------------------------jumpcheck = 0 //reset for next press

Isn’t there a simpler way to do this thing? I was thinking about using a one-off impulse, making the cube jump a fixed amount. Considering it’s a mechanic used when the cube is touching the ground plane, it would have the same setting as a sudden force being applied in the +Z axis.

What I don’t get is why it looses momentum when being pushed forward…

You’re applying linear velocity in different ways (i.e. executing object.setLinearVelocity) depending on the situation. It’s generally better to change variables and apply the linear velocity once. Here’s a modified example.

see_cube_move2.blend (446 KB)

I also fixed the jumping by adding it into the script. I set the Z-axis movement to be the jump speed (defined right above the key press check) only if you’re on the ground. I made this by adding a thin cube below the player that is a sensor-type object. Sensors detect pretty much all other objects. When you’re on the ground, the sensor returns positive. When you jump, the sensor doesn’t return positive. So, if the sensor’s positive and you press jump, it sets the Z-speed of the object to be the jump speed.

This could also be done with a ray cast or two.

That’s perfect! You’re the man! I’m processing all the info now. I was thinking it just might be something related to the execution of multiple things at once, and only one was processed. My failure in understanding basic dynamics was guiding me wrong.

I expect a few questions coming in (What’s that Sensor for example), but the doors are wider now for me to continue my fanfare.

AAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!!

You made me realize how different things are from my point of view. You firstly process all the logic and then finally impose the changes in the final mainChar.localLinearVelocity = [x, y, z].

I was changing them on all checks, mixing and matching different changes at different times.

Silly me. Thanks for the eye opener.

PS: I’m looking forward for Valchion (been following it on TIGS Forum). It was your game that made me come here and have the courage to enter this world with the mentality to actually learn it, so it’s kind of an happy coincidence that you provided me with this precious information.

Regards.

While you can do multiple setLinearVelocity() calls at a time, it’s easier to see how the physics engine and everything will interact with each other by setting the values all at the same time. The sensor physics type is basically a ghost and has no dynamics (I think), though it detects collisions with all other objects (except possibly other sensors). You can do the same thing with Dynamic or Static objects set to Ghost, but I think they’ll only detect collisions with Dynamic objects. In other words, a sensor senses collisions with pretty much all objects, unlike other dynamic types.

And no problem. Hah, I’m a little embarrassed, though I’m glad that people are looking forward to Valchion.

If you need any further help, there’s an IRC channel for BGE users at irc://freenode/gameblender and a Reddit thread as well.

Logic brick solution ignored?

@BluePrintRandom,
I thank you for your suggestion. I like the form that I can control the time the jump takes and how easy it is to expand it to double-jump and wall jump. It has been noted as a solution, but considering the possible complexities that arise when developing a game (even something as simple as this), I decided to make the leap towards scripting, as it will provide a more robust solution for the future.

Don’t think I ignored your suggestion. It is well guarded.