Reputation: 199
This is my charecter in love2d while jumping. It looks fine but..
when I get on the ground it looks like this.
I figured out that it may have some thing to do with the img, that it doesn't fill out the entire square. So it is simply is out of the collision square.
Thats the square it should have been in, but since im quite new to programing I cant figure out how to do it. I have been searching for a solution but cant find one ATM.
This is my love.load
function love.load()
love.graphics.setBackgroundColor( 204, 255, 204 )
crazy = love.graphics.newImage("player.png")
pwidth = crazy.getWidth
pheight = crazy.getHeight
AdvTiledLoader.path = "maps/"
map = AdvTiledLoader.load("map.tmx")
map:setDrawRange(0, 0, map.width * map.tileWidth, map.height * map.tileHeight)
camera:setBounds(0, 0, map.width * map.tileWidth - love.graphics.getWidth(), map.height * map.tileHeight - love.graphics.getHeight() )
world = {
gravity = 1536,
ground = 512,
}
player = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -1024,
speed = 512,
flySpeed = 700,
state = "",
h = 32,
w = 32,
standing = false,
}
function player:jump()
if self.standing then
self.y_vel = self.jump_vel
self.standing = false
end
end
function player:right()
self.x_vel = self.speed
end
function player:left()
self.x_vel = -1 * (self.speed)
end
function player:stop()
self.x_vel = 0
end
function player:collide(event)
if event == "floor" then
self.y_vel = 0
self.standing = true
end
if event == "cieling" then
self.y_vel = 0
end
end
And my love.draw
function love.draw()
camera:set()
love.graphics.draw(crazy, player.x , player.y)
love.graphics.setColor( 255, 255, 255 )
map:draw()
camera:unset()
end
If you need to see my collision or anything else just ask and I will paste it below :)
I really apriciate your help thanks!
Upvotes: 0
Views: 209
Reputation: 51
First of all, I assume you want pwidth/pheight to be numbers, not the get functions.
pwidth = crazy.getWidth()
pheight = crazy.getHeight()
The image is drawn from it's topleft corner, but I'm guessing that your collision logic has the hitbox centered on the player's position.
You can either compensate by changing the coordinate of the draw function, or pass in an origin offset to the image. The advantage of specifying the origin offset is that you can then rotate and scale relative to that origin.
love.graphics.draw(crazy, player.x - pwidth/2 , player.y - pheight/2)
or
rotation, scalex, scaley = 0, 1, 1
love.graphics.draw(crazy, player.x, player.y, rotation, scalex, scaley, pwidth/2, pheight/2)
Upvotes: 0
Reputation: 1017
What is most likely happening is that the you have the x,y co-ordinate as the centre of the sprite. So it's stopping when the map meets the centre.
As you're drawing the sprite before the map, you only see the bit not covered by the map.
An easy way of proving this is to draw the sprite after the map and you should see the whole sprite over the map.
There would be a couple of ways to resolve this. Either leave the collisions where they are and draw the sprite with ox=-width/2,oy=-height/2.
Alternatively have the collision at the at the bottom of the sprite by adding width/2 and height/2 to the collision point.
If this doesn't help then we'll probably need the collision logic.
Upvotes: 1