Log In  
Follow
rbndr
agametopassthetime
by
[ :: Read More :: ]

i've tried to make a function that scales and offsets sprites to simulate a camera zoom. The way it works is by pressing z and x will increase or decrease the pixel size, each object has x and y values, and sx and sy which are scaled and offset based on the pixel size value.

in this demo the black cats are based on the x and y values, the blue cats are based on the sx and sy values. The scaled blue cats movement is very jittery, especially when accelerating and decelerating, the non scaled black cats are not jittery.

So I think the controls are changing the x and y values correctly, and there isn't a problem with the camera being out of sync, it must be a problem with getting the sx and sy values from my x and y ones. Looking at it, it is like they are not moving in unison, instead the movement is rippling through the objects one by one.

it is the scale_x and scale_y functions that create the sx and sy, and these functions are called in the 'move' function

Any idea what I am doing wrong?

thanks

Cart #camerazoom-0 | 2024-06-26 | Code ▽ | Embed ▽ | No License
2

--function to scale and offset 
--the x co-ordinate when pixel
--size is changed

function scale_x (x)
local newx=x*pixels
local offset=newx-x
local x1=x+newx-pixels*28
local zero=camera_x+64-pixels*(camera_x+64)
x=(zero+newx)
return(x)
end

--function to scale and offset 
--the y co-ordinate when pixel
--size is changed

function scale_y (y)
local newy=y*pixels
local offset=newy-y
local y1=y+newy-pixels*28
local zero=camera_y+64-pixels*(camera_y+64)
y=(zero+newy)
return(y)
end

--moves all objects
--calls boxhit function that
--checks colisions

function move(obj) 

if obj==pl then
if btn(0) then 
obj.dx-=obj.acc --move left amount
obj.flp=true
sound=true
end

if btn(1) then 
obj.dx+=obj.acc --move right amount
obj.flp=false
sound=true
end

if btn(2) then 
obj.dy-=obj.acc --move up amount when jump
sound=true
end

if btn(3) then 
obj.dy+=obj.acc --move up amount when jump
sound=true
end
end

--reduce vertical movement by gravity each update
--obj.dy+=obj.gravity

--reduce horizontal movement by friction
obj.dx*=obj.friction
obj.dy*=obj.friction

--limit max speed in all directions
obj.dy=mid(-obj.max_dy,obj.dy,obj.max_dy)    
obj.dx=mid(-obj.max_dx,obj.dx,obj.max_dx)

--new locations if movement applied
obj.newx=obj.x+obj.dx
obj.newy=obj.y+obj.dy

--check whether new locations collide with other objects
--will adjust newx/newy positions to not overlap collided object

for v in all(things) do
if obj!=v then
--check collision of new vertical
-- box_hit(obj,v,"y")
obj.y=obj.newy 
obj.sy=(scale_y(obj.y-obj.h))

end
end

for v in all(things) do
if v!=obj then
--check collision of new horizontal
-- box_hit(obj,v,"x")
obj.x=obj.newx
obj.sx=(scale_x(obj.x-obj.w))
end
end
end

function animate(obj)
obj.s+=(abs(obj.dx)/3)
if obj.s>4 then obj.s=0 end    
if obj.dx>0 then obj.flp=false elseif obj.dx<0 then obj.flp=true end
end 

function _init()

camera_x=0
camera_y=0

--used as camera zoom
pixels=1

--create table for objects
things={}

--make objects
function make_thing(x,y
,w,h,clr,g,f,s)
thing={

x = x,-- horizontal position
y = y,-- vertical position
w = w,-- width
h   = h,--  height
dx  = 0,-- horizontal movement
dy  = 0,--  vertical movement  
sx  = x,--scaled/offset to draw
sy  = y,--scaled/offset to draw     
max_dx=2, -- max horizontal speed
max_dy=2, -- max vertical speed
acc=0.5, -- acceleration
boost=6, -- boost when jump
colour=clr,
gravity=g,
friction=f,
s=s,
flp=true
}

--add object to things table
add(things,thing)

return thing
end

--make player
pl=make_thing(64,64,4,4,1,0.3,
0.95,64)

--make some npcs
for i=1,16 do
make_thing(flr(64+cos(i/16)*25),
64+(sin(i/16)*25),4,4,i,0.3,1,
64)

end
end

function _update()

--move and animate
for v in all(things) do 
move(v)
animate(v)
end

--update camera
camera_x=(pl.x-64)
camera_y=(pl.y-64) 

--zoom in and out
if btn(4)  then 
pixels=pixels*1.1 end
if btn(5)  then 
pixels=pixels*0.9 end

end

function _draw()
cls(7)
camera(camera_x,camera_y)

--draw sprites based on 
--sx and sy

for v in all(things) do
                pal()
    sspr( flr(v.s)*8, 
    32, 
    8, 
    8, 
    (v.x)-v.w,
    (v.y)-v.h,
    (v.w*2),
    (v.h*2),
    v.flp)

    pal(1,12)
    sspr( flr(v.s)*8, 
    32, 
    8, 
    8, 
    (v.sx),
    (v.sy),
    ((v.w*2)*pixels),
    ((v.h*2)*pixels),
    v.flp)
end

--border
rect(camera_x,camera_y,camera_x+127,camera_y+127,12)
rect(camera_x+1,camera_y+1,camera_x+126,camera_y+126,13)

end
P#150459 2024-06-26 17:37

[ :: Read More :: ]

Cart #rbndr-1 | 2024-06-26 | Code ▽ | Embed ▽ | No License
4

because there can never be enough Celeste mods, just changed the colour palette a bit and put in a self insert main character

edit: fixed Maddy's name

P#145146 2024-03-30 17:53 ( Edited 2024-06-26 17:51)

[ :: Read More :: ]

edit: latest version all objects check all objects and transfer momentum

Cart #nonmapbasedcollisions-2 | 2024-04-05 | Code ▽ | Embed ▽ | No License
2

edit: forgot cart

Cart #nonmapbasedcollisions-0 | 2024-02-20 | Code ▽ | Embed ▽ | No License
6


edit: solved!
Cart #nonmapbasedcollisions-1 | 2024-02-20 | Code ▽ | Embed ▽ | No License
1

Can anyone advise me on where I am going wrong with my collission detection. I want to be able to use table objects instead of map tiles and flags as I want to be able to continuously scroll for wider than the map allows.

I have pieced the following together from various sources (omitted the init and draw parts of the code, they just set up the various objects, but are in the carts code). It is using the method of checking whether the distance between the midpoints of two objects are smaller than the sum of the halves of the objects for both dimensions.

When something affects the players movement, either buttons or gravity, the new Y and X positions are run through a collision detection which loops through every object, with y then x positions being checked within each loop, if a movement results in a collision the new X or Y position is adjusted to the max position that doesn't overlap and momentum is stopped. If there is no collision the new X and Y positions become the actual X and Y positions, if there is a collision the new X and Y positions are adjusted before becoming the actual X or Y positions.

It almost works except for on corners where the player can get stuck and it judders. I've set up the cart so that it is easy to recreate the problem by falling/jumping along the vertical side of the squares while pressing left or right.

I guess this is where within a loop both X and Y are adjusted putting them back to a position where it will keep happening while those directions are maintained. I assume I have to give priority to one axis but I can't figure out how to do this. I was wondering what the best approach is for this.

Also is this approach on collisions generally ok or is there an easier or more efficient way, will it use up too much processing to do an entire map this way? I have tried to figure this out but most of the collision guides seem to be either map tiles and flags, or simple one pixel by pixel movement, or comparing only two objects at a time.

thanks

here

--move objects
function _update()
move(pl)

end

--move and collission functions

function move(obj) 

 if (btn(0)) then 
 obj.dx-=obj.acc --move left amount
 end

 if (btn(1)) then 
 obj.dx+=obj.acc --move right amount
 end

 if (btnp(5)) then 
 obj.dy-=obj.boost --move up amount when jump
 end

 --reduce vertical movement by gravity each update
 obj.dy+=gravity
 --reduce horizontal movement by friction
 obj.dx*=friction

 --limit max speed in all directions
 obj.dy=mid(
 -obj.max_dy,
 obj.dy,
 obj.max_dy)

 obj.dx=mid(
 -obj.max_dx,
 obj.dx,
 obj.max_dx)

    --new locations if movement applied
 obj.newx=obj.x+obj.dx
 obj.newy=obj.y+obj.dy

 --check whether new locations collide with other objects
 --will adjust newx/newy positions to not overlap collided object

        for v in all(things) do
        if v!=pl then
        --check collision of new vertical
    box_hit(obj,v,"y")
    --check collision of new horizontal
    box_hit(obj,v,"x")
            --apply new positions (adjusted to stop before collide)
    end
    end

 obj.y=obj.newy 
 obj.x=obj.newx     

 obj.y=mid(0,obj.y,120)
 obj.x=mid(0,obj.x,120)

end

function box_hit(obj,obj2,axis)

--compares one object x/y/w/h to another  
--axis is which dimension of movement being checked
--hitbox different depending on direction

  if axis=="y" then
  y1=obj.newy
  x1=obj.x

  elseif axis=="x" then
  y1=obj.y  
  x1=obj.newx
  end

  w1=obj.w
  h1=obj.h
  x2=obj2.x
  y2=obj2.y
  w2=obj2.w
  h2=obj2.h

  --collision check method 
  --if distance between midpoints greater than 
  --the sum of the two box halves

  --distance mid points
  local xd=abs((x1+(w1/2))
  -(x2+(w2/2)))
  --sum of box widths/2
  local xs=w1*0.5+w2*0.5
  --distance mid points
  local yd=abs((y1+(h1/2))
  -(y2+(h2/2)))
  --distance mid points
  local ys=h1/2+h2/2

if xd<xs and 
   yd<ys then 

--is a collission    

  if axis=="y" then
    --kill vertical momentum
                obj.dy=0
    if y1<y2 then
    --if collide from above limit movement to last non collide point 
    obj.newy=flr(y1-(ys-yd)) 
    else 
    --if collide from below limit to lowest point of 2nd object
    obj.newy=ceil(y2+h2) end

  else
    --kill horizontal momentum
                obj.dx=0

    if x1<x2 then
    --if colliding from left
    obj.newx=flr(x2-obj.w) 
    else 
    --if colliding from right
    obj.newx=ceil(x2+w2) end

 end
 end 
P#141692 2024-02-20 19:36 ( Edited 2024-04-05 11:04)

[ :: Read More :: ]

this is my first attempt at doing anything in pico 8, or any game. there isn't much game play yet though.

updated idle animation in v2

Cart #agametopassthetime-2 | 2023-11-01 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
33

P#136700 2023-10-31 20:25 ( Edited 2023-11-01 14:55)