Changing Mesh Rect Origin

Hello. When I use addRect() for a mesh, it creates the rect with the pos centered in the rect. I’m looking for a way to change the rect mode to center it on the bottem left corner. I suspected rectMode() would do this but apperently it works just with rects that are not of the meshy variety. Would anyone one know how to acomplish this?

You can override the mesh function “addRect” to make the function so it follows the definition of “rectMode”.

duplicate

@Luatee was suggesting that you make a function where you pass in a mesh, the position, and size of the rect you want to make and that function will take your mesh and create the rect for you with the position you want. So I assume you want the rect to be handled by the bottom left edge. To do that, the position would be vec2(pos.x+size.x/2,pos.y+side.y) and the size would be the normal size. Pretty simple stuff.
I was suggesting doing the same thing, but instead overriding the entire addRect function, which I can explain if you don’t get how to do.

You can create a function that makes a mesh and set’s it’s positions at minus half of the width and height of the rectangle, this makes it act like the default rectMode() setting when using rect(). Or make a class instead of a function and handle the positions with setRect as well.

Could you explain the exact steps of doing that? I honestly didn’t get most of what you said. It sounds like your both saying the same thing of sorts.

So I tried out the adjusted pos method without really understanding how it would work out and I seem to have done something wrong. Could you take a look to the code and see what I did wrong? I learn best from seeing examples of concepts.

IdRect = class()

function IdRect:init(col, pos, dim)
    self.pos = pos
    self.col = col
    self.mesh = mesh()
    -- self.mesh:addRect(pos.x, pos.y + ITEM_HEIGHT, dim.x, dim.y)
    self:CreateMesh(self.mesh, pos, dim)
    self.mesh:setColors(self.col)
end

function IdRect:draw()
    pushStyle()
    fill(self.col)
    self.mesh:draw()
    fill(0, 0, 0, 255)
    strokeWidth(10)
    line(self.pos.x, self.pos.y, self.pos.x, self.pos.y)
    popStyle()
end

function IdRect:CreateMesh(m, pos, dim)
    -- Creates a rect in the given mesh that is centerted at the bottem left corner
    m:addRect(pos.x + dim.x/2, pos.y + dim.y, dim.x, dim.y)
end

@Goatboy76 Here’s an example to add a mesh rect using the lower left corner coordinates.


function setup()
    m=mesh()    -- create mesh
    aRect(100,100,120,60)   -- add rect with corner coordinates
    m:setRectColor(1,255,0,0)   -- set rect color
    
end

function draw()
    background(40,40,50)
    m:draw()    -- draw mesh
end


function aRect(x,y,w,h)
    m:addRect(x+w/2,y+h/2,w,h)  -- add rect with lower left corner coordinates
end

@dave1707 I get it now , thanks. Why I din’t see that before… @Zyot just left out dividing the height by 2 when adding it to the y pos. I got to caught up in scouring the reference material to realize that the solution was really in the cupcake all along :frowning: Thanks tho

@Goatboy76 my mistake the default rectMode is bottom to top not top to bottom!

@Goatboy76 - Sorry, typed on an iPhone.

@Goatboy76 Here’s my example changed to a class.


function setup()
    a=aRect(100,100,120,60,color(255,0,0))
    b=aRect(220,160,120,60,color(0,255,0))
    c=aRect(340,220,120,60,color(0,0,255))
end

function draw()
    background(40,40,50)
    a:draw()
    b:draw()
    c:draw()
end

aRect = class()

function aRect:init(x,y,w,h,c)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.c=c
    self.m=mesh()
    self.m:addRect(self.x+self.w/2,self.y+self.h/2,self.w,self.h)
    self.m:setRectColor(1,c)    
end

function aRect:draw()
    self.m:draw()
end

@Luatee I’m not quite sure what you mean by that

@Zoyt It’s cool

@dave1707 Thats a good example, thanks.

I was able to apply this to do what I wanted to do. Thanks ya’ll.

@Goatboy76 default rectMode’s position is in the left bottom corner of the rectangle not the top right as I was thinking,