Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Noob 2D Movement Question?

All about the StoneScript

Re: Noob 2D Movement Question?

Postby Guinner » 01 Apr 2012, 14:44

Hey, just went to create a thread and saw that this was still active so:
My problem is I added two Billboards on either side of the character (So it doesn't fall off the screen.) I also changed mass, bounce, friction and movement of the character to stop it from bouncing everywhere and to slow the movement. But the character is no longer jumping :c I have the following code:

Char_AI
Code: Select all
function char_AI.onJump ( )
--------------------------------------------------------------------------------
local height = 20 --change this to a number that suits you
local x,y,z = object.getTranslation (this.getObject(), object.kGlobalSpace) --get object coordinates in global space
local hHitObject, nHitDist, nHitSurfaceID = scene.getFirstHitCollider ( application.getCurrentUserScene(), x, y, z, 0, -1, 0, height )

local upforce = 75 --change to a number that suits you
if not hHitObject then
    dynamics.addLinearImpulse ( this.getObject(), 0, upforce, 0, object.kLocalSpace )
end
   
--------------------------------------------------------------------------------
end


Under MainAI.onKeyboardDown
Code: Select all
else if kKeyCode == input.kKeyW then
    object.sendEvent (hPlayer, "char_AI", "onJump") --1 forward, 0 stand still, -1 backward
end


There's no errors, but it's not jumping either :/
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 01 Apr 2012, 18:10

what do you mean by billboads? please provide a small screenshot/video/STE archive. something might be intersecting with the player dynamics sphere body, or you set the damping too high, or the trace constantly... on second thought, why is there a "not" in front of hHitObject.

the idea of the trace: it scans the area under the player's feet. so if there is NOT an object hit by the ray, it should NOT jump, but if there IS an object, it SHOULD jump. height should be small (like 0.3 instead of your 20 meters). try removing the "not".
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

Re: Noob 2D Movement Question?

Postby Rotundjere2 » 01 Apr 2012, 19:32

thanks, this is working now..

probably because I put the model to close with plane/ground (the model make collision with ground first and then jumping to high)
User avatar
Rotundjere2
Junior Boarder
Junior Boarder
 
Posts: 31

Re: Noob 2D Movement Question?

Postby Guinner » 02 Apr 2012, 03:04

I don't have very good internet so uploading anything takes forever, sorry.
I took out the 'not' to no avail.
The setup is as follows:
Camera< Wall Character Wall
So the camera is looking in that direction -> There is an invisible wall, then the character which is just standing on a square, there is then another invisible wall just behind this square. This stopped the character from falling off, bouncing erratically and just makes it the 2D movement, (Unless there's a better way?)
Character left and right movement still works but there is no jump? I'd really appreciate the help, you guys really are amazing!
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby Rotundjere2 » 02 Apr 2012, 04:00

Guinner wrote:I don't have very good internet so uploading anything takes forever, sorry.
I took out the 'not' to no avail.
The setup is as follows:
Camera< Wall Character Wall
So the camera is looking in that direction -> There is an invisible wall, then the character which is just standing on a square, there is then another invisible wall just behind this square. This stopped the character from falling off, bouncing erratically and just makes it the 2D movement, (Unless there's a better way?)
Character left and right movement still works but there is no jump? I'd really appreciate the help, you guys really are amazing!




This code is working very well :D

Code: Select all
--------------------------------------------------------------------------------
function char_AI.onJump ( )
--------------------------------------------------------------------------------
local height = 0.3 --change this to a number that suits you
local x,y,z = object.getTranslation (this.getObject(), object.kGlobalSpace) --get object coordinates in global space
local hHitObject, nHitDist, nHitSurfaceID = scene.getFirstHitCollider ( application.getCurrentUserScene(), x, y, z, 0, -1, 0, height )

local upforce = 50 --change to a number that suits you
if (this.jumping() == true) and hHitObject then
    dynamics.addLinearImpulse ( this.getObject(), 0, upforce, 0, object.kLocalSpace )
    this.jumping(false)
end
   
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
User avatar
Rotundjere2
Junior Boarder
Junior Boarder
 
Posts: 31

Re: Noob 2D Movement Question?

Postby Guinner » 02 Apr 2012, 08:48

Rotundjere2 wrote:
This code is working very well :D

Code: Select all
--------------------------------------------------------------------------------
function char_AI.onJump ( )
--------------------------------------------------------------------------------
local height = 0.3 --change this to a number that suits you
local x,y,z = object.getTranslation (this.getObject(), object.kGlobalSpace) --get object coordinates in global space
local hHitObject, nHitDist, nHitSurfaceID = scene.getFirstHitCollider ( application.getCurrentUserScene(), x, y, z, 0, -1, 0, height )

local upforce = 50 --change to a number that suits you
if (this.jumping() == true) and hHitObject then
    dynamics.addLinearImpulse ( this.getObject(), 0, upforce, 0, object.kLocalSpace )
    this.jumping(false)
end
   
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------

I'm sorry, but how do you declare this under the keyboardpress? Under main_AI. I feel so silly asking so many questions!
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 03 Apr 2012, 09:58

you don't. this is the onJump handler for your char AI.

to declare a member variable like this.jumping(), go to the AI Model Module (the one with the brain), open your char_AI, then click the "+" under variables and call it "jumping" and set it to boolean, initial value "false".

Image
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

Re: Noob 2D Movement Question?

Postby Guinner » 03 Apr 2012, 13:25

Ahh, I phrased my questions very badly. What I meant was how do I say 'When W is pressed, make jumping true.' Like this, although this one doesn't work:
Code: Select all
else if kKeyCode == input.kKeyW then
    object.sendEvent (hPlayer, "char_AI", "onJump", true)
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 03 Apr 2012, 13:45

like so:
Code: Select all
if kKeyCode == input.kKeyW then
    object.setAIVariable ( hChar, "char_AI", "jumping", true )
    object.sendEvent (hPlayer, "char_AI", "onJump" )
end
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

Re: Noob 2D Movement Question?

Postby Guinner » 08 Apr 2012, 04:34

Main_AI_Handler_onKeyboardKeyDown.lua:16: undeclared variable : hChar
Now I tried fixing this myself :c Everything from adding it as a objectvariable on main_AI and Char_AI to declaring it as a local variable the same what we did hPlayer. But I just can't figure out where it goes.
Sorry, could I get some help on this one again?
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 08 Apr 2012, 13:45

oh well. i guess it's just easier if i made a small sample game... give me 20 minutes
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

Re: Noob 2D Movement Question?

Postby Guinner » 08 Apr 2012, 14:00

Thanks heaps man, sorry to ask so much.
If it's too much trouble then please dont. But if so, it'd be awesome to see how the proper 2D is done.
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 08 Apr 2012, 15:23

quick update: i have movement working, however, addLinearImpulse produces rather unnatural results. let me tinker with it a little longer, to get "proper" movement.
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

Re: Noob 2D Movement Question?

Postby Guinner » 08 Apr 2012, 15:25

I'll be waiting in anticipation!
Guinner
Junior Boarder
Junior Boarder
 
Posts: 36

Re: Noob 2D Movement Question?

Postby broozar » 08 Apr 2012, 18:01

ok, sorry. took me longer than i expected, not the coding, but making the movement feel natural - whatever that means in a 2d world where characters are supposed to be able to jump 5 times their own height.

youtube video of how it is supposed to look: http://youtu.be/i-71R-AuOgk

the project STE: http://caffier.net/demo2d.ste
User avatar
broozar
Platinum Boarder
Platinum Boarder
 
Posts: 3508
Location: Berlin - Germany

PreviousNext

Return to Game programming