Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
February 10, 2012, 12:36:40 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Check out the Code Section!
Home Help Search Shop Login Register
Digg This!
Pages: [1]
Print
Author Topic: Help with code  (Read 1596 times)
baseball3
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7
286.00 points

View Inventory
Send Money to baseball3


View Profile
« on: March 06, 2010, 06:56:40 PM »

Hi everyone i need help with this code. The collision does not work right...

Code:
brown = Color.new(139,69,19)
green = Color.new(0,255,0)
white = Color.new(255,255,255)
player = Image.load("player.png")
background = Image.load("background.png")
player1 = player
block = Image.createEmpty(22,22)
block:clear(brown)
playerHeight = 32
playerWidth = 32
Block = {}
Block[1] = { x = 100, y = 220, height = block:height(), width = block:width() }
Block[2] = { x = 300, y = 220, height = block:height(), width = block:width() }
Block[3] = { x = 200, y = 220, height = block:height(), width = block:width() }


ground = Image.createEmpty(480,10)
ground:clear(green)
player = {}
player.gravity = 230
player.y = 230
player.x = 50
player.jumpspeed = 5
player.jumpstate = "ground"
function collisionCheck(object)
if (player.x + playerWidth > object.x) and (player.x < object.x + object.width) and (player.y + playerHeight > object.y) and (player.y < object.y + object.height) then
player.x = oldx
player.y = oldy
end
end
while true do
pad = Controls.read()
oldx = player.x
oldy = player.y
screen:clear()


if pad:left() and player.x > 0 then
player.x = player.x - 2
end
if pad:right() and player.x < 450 then
player.x = player.x + 2
end
if pad:cross() and player.jumpstate == "ground" then player.jumpstate =
"jumping" end
if pad:select() then
Image:save( "Screenshot.JPEG")
end


collisionCheck(Block[1])

if player.jumpstate == "jumping" then
player.jumpspeed = player.jumpspeed - 2
player.gravity = player.gravity - player.jumpspeed - 10
end
if player.gravity < 0 then
player.jumpstate = "falling"
end
if player.gravity < 230 and player.jumpstate == "falling" then
player.gravity = player.gravity + (player.jumpspeed + 1)
end
if player.gravity == 230 then
player.jumpspeed = 10
player.jumpstate = "ground"
end

if player.gravity > 600 then player.gravity = 230 end
player.y = player.gravity
screen:blit(0,0,background)
screen:blit(player.x,player.y,player1)
screen:blit(0,262,ground)
for a = 1,3 do
screen:blit(Block[a].x,Block[a].y,block)
end

screen:print(10,10,"X:"..player.x.."Y:"..player.y,green)
screen:print(10,20,"Jumpstate:"..player.jumpstate,green)

screen.waitVblankStart()
screen:flip()
end

Thanks in advance.

P.S. I know it bad Smile
Logged

Semper fi


AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #1 on: March 07, 2010, 03:53:28 AM »

Your variable names could use some work.  You load an image into 'player' and then later declare a table as 'player'.  Also you use 'block' as an image and 'Block' as another table.  I don't think Lua is case sensitive (it's been a while) but that's a recipe for confusion.  Try names like 'player_sprite' and 'player_table' to clarify things and avoid variable conflicts.

I'd recommend correcting your player inertia handling first here.

The player object should have an x_inertia and y_inertia.  At rest they'd both be zero.  Negative x_inertia would be moving left, positive to the right, y is negative upwards and positive downwards, etc.  Gravity would be a constant that you can adjust to your liking and would be added to the y_inertia every frame.  A jump_potential variable can be added to the player which will "recharge" to a set value when the player is on the ground.  When a jump is triggered the jump potential is subtracted from the y_inertia and reduced every frame until it hits zero.

For instance (pseudocode):

Code:
gravity = 2

--this bit should actually be handled during collision checking during the previous frame rather than here
if on_ground then
  player.jump_pot = 4
else
  if player.jump_pot > 0 then
    player.jump_pot -= 1
  end
end

if cross then
  player.y_inertia -= player.jump_pot
end
player.y_inertia += gravity

That's not in Lua, but I think it gets the point across.  You'll want to 'cap' the inertia values to ensure that they are never larger than the size of a collidable surface or else you'll get moving so fast you'll pass right through blocks without colliding.

I'd also recommend using 'new_x' and 'new_y' rather than 'oldx'/'oldy' to help with the collision handling.  Ex:

Code:
friction = 1
new_x = player.x + player.x_inertia
if (player.x_inertia > 0) and ((new_x + player.width) > object.x) then
  new_x = object.x - player.width
  player.x_inertia -= friction
end
if (player.x_inertia < 0) and (new_x < (object.x + object_width)) then
  new_x = object.x + object_width
  player.x_inertia += friction
end
player.x = new_x

You may want to forego inertia use on the x axis and opt for direct control, but on the y axis it's the best way to get a 'true' gravity vs jump dynamic.

Also, this line bothers me:
Code:
if player.gravity > 600 then player.gravity = 230 end

That 600 doesn't belong there I think, and 230 is pretty darn high, to be honest.  A good limit for y speeds would probably be something closer to 10 but you'll have to fiddle.

It's a little difficult to get used to at first, but you're doing well for a first go.

Sorry if that's gibberish.  I'm very tired and my brain is making a buzzing sound.  I'll check back in later and maybe show a proper example.
Logged
baseball3
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7
286.00 points

View Inventory
Send Money to baseball3


View Profile
« Reply #2 on: March 07, 2010, 10:03:54 AM »

Thanks very much Very Happy
Logged

Semper fi
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #3 on: March 07, 2010, 06:46:48 PM »

I don't know if you're familiar with Ruby or not, but I can post a super mario demo (or just the scripts) that I made in RPG Maker XP.  Ruby is a scripting language, like Lua, so it's pretty easy to follow, but it's very object-oriented, so if you've never used OOP then it may just confuse you.
Logged
baseball3
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7
286.00 points

View Inventory
Send Money to baseball3


View Profile
« Reply #4 on: March 11, 2010, 08:07:09 AM »

That might help if you could post i would be thankful Smile
Logged

Semper fi
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #5 on: March 12, 2010, 12:12:25 AM »

I'll just post the scripts, since you probably don't have RMXP.

There are some references to database defined variables in the mapping stuff, and the input module is part of the built-in RGSS extension, but it's rather straightforward.  The input functions:

Input.update - captures input state
Input.trigger(key) - returns true if the key is pressed in this frame but not the previous frame
Input.dir4 - returns one of the following (corresponds to num pad directions):
0 no direction
2 down
4 left
6 right
8 up
Input.press(key) - returns true if key is pressed during this frame

The Sprite class (some of my classes inherit from it) is built in and has the following:
Properties:
bitmap - the sprite's assigned texture
src_rect - a rect (x, y, width, height) within the bitmap from which to get the image
x, y, z - screen coordinates of the sprite, screen is 640 x 480
mirror - boolean value used to flip the image
Methods:
new(viewport) - class constructor method
dispose - disables the sprite and actively frees any resources it was using (rather than waiting for garbage collector)
update - updates built-in 'flash' effect.  I don't think I used this, but I may have

I think that's all I used from Sprite.

Graphics.update - renders all Sprite and Plane objects according to their current states, in the order they were created.  Waits for vsync and flips screen

General Ruby notes:

Ruby uses the $ prefix for global variables and the @ prefix for object-local variables.
attr_reader is a macro to allow an object variable to be read from outside the class by calling it as a method:
attr_accessor is the same, but allows the variable to be modified as well

Code:
class Test
  attr_reader :var #without this line the variable is private
  def initialize #defines the constructor (method gets internally alised to 'new')
    @var = 3
  end
end

temp = Test.new
p temp.var #prints out '3', the value of the object's @var

Ruby inheritance is done during class declaration, ie:

class ChildClass < SuperClass

starts defining a class called ChildClass that inherits from SuperClass.  If a method in ChildClass has the same name as a method in SuperClass then it can call the method 'super' in order to invoke the same method within the superclass.

Code:
class Animal
  def report
    print "This is an animal."
  end
end

class Dog < Animal
  def report
    super #invokes the 'report' method of the superclass, 'Animal'
    print " It is a dog."
  end
end

temp = Animal.new
temp.report #prints "This is an animal."
other = Dog.new
other.report #prints "This is an animal. It is a dog."

If there's anything else that's confusing I can explain it for you.

I've just plucked the scripts out and pasted them into a file.  Since it's small I'll include the project itself as well in case you want to get the RMXP trial version or something and check it out.  Here's your link:

http://mayobe.webs.com/mario_rmxp.rar
« Last Edit: March 12, 2010, 12:31:10 AM by AlphaDingDong » Logged
baseball3
Newbie
*

Karma: +1/-0
Offline Offline

Posts: 7
286.00 points

View Inventory
Send Money to baseball3


View Profile
« Reply #6 on: March 12, 2010, 08:07:16 AM »

Ohtay thanks Very Happy
Logged

Semper fi
Pages: [1]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.241 seconds with 30 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com