News:

Function Finder  Find all the functions within source code files

Main Menu

Manually Drawing Shapes

Started by wildbill, October 21, 2010, 01:41:46 PM

Previous topic - Next topic

wildbill

I am working on a game editor. The plan is to be able to position an object, then draw a shape for collision purposes (much like the Play Shape tool only in game). I'm trying for a WYSIWYG type editor. I have grabbed a shape drawing example that Kevin did a long time back and it works fine up to a point. When the shape is rendered though, it actually is placed offset from 0,0. The easiest way to explain this is to run the example code. Towards the end you can rem out the DrawShape statement and see the difference between the two. I guess my question is why is the vertex being offset?
Do
cls 0
Numb_of_line_sections=0

Dim Vertex_Xpos#(1000)
Dim Vertex_Ypos#(1000)
MouseButtonClicked=0

` ==============================================
` CReate A Shape WIth the Mouse
` ==============================================

repeat

  text 10,0,"Create Shape OutLine"
 
  `if mouse clicked
  if MouseButtonClicked=0
  if (mousebutton()&1)=1
    xpos=mousex()
    ypos=mousey()
    ` check If this is the first click or not
    if Numb_of_line_sections=0
    ` remember the first click position
    First_xpos=xpos
    First_ypos=ypos
    Dot xpos,ypos
    else
    line old_xpos,old_ypos,xpos,ypos
    endif
    old_xpos=xpos
    old_ypos=ypos
    Vertex_Xpos#(Numb_of_line_sections)=Xpos
    Vertex_Ypos#(Numb_of_line_sections)=Ypos
    inc Numb_of_line_sections
    MouseButtonClicked=1
  endif
  endif

  ` Check the mouse button to make sure it's not being held down
  ` ============================================================
  if (mousebutton()&1)=0
  MouseButtonClicked=0
  endif

  Sync

until (mousebutton()&2)=2

; =========================================
; CREATE A VECTOR SHAPE Form This POint Set
; ==========================================


  CreateShape 1,Numb_of_line_sections,Numb_of_line_sections
 
  For lp=0 to Numb_of_line_sections-1
  SetShapeVertex 1,lp,Vertex_Xpos#(lp),Vertex_Ypos#(lp)
  Edge=lp
  v2=LP+1
  if v2>(Numb_of_line_sections-1) then V2=0
  SetShapeEdge 1,Edge,lp,v2
  next

` ==============================================
` Render The Shape
` ==============================================

Repeat
  cls 0
  setcursor 0,0

  Text 0,0,"Number of Sides:"+str$(Numb_of_line_sections-1)
  text 0,20, "Press Any Key TO Start Again:"
text 0,40,"Vert X : "+str$(Vertex_Xpos#(0))
`==========================================================
`***************This is where the question is???????????
DrawShape 1,Vertex_Xpos#(0),Vertex_Ypos#(0),1
  `DrawShape 1,Vertex_Xpos#(0)-Vertex_Xpos#(0),Vertex_Ypos#(0)-Vertex_Ypos#(0),1
`==========================================================
  Sync
until Scancode()<>0

loop

kevin


    DrawShape 1,Vertex_Xpos#(0),Vertex_Ypos#(0),1

   Why are you drawing at screen cords of the first vertex ?   

   If the first vertex is at 100x,50y (local space) and the you draw this shape at position 200,200.  Then this vertex will be drawn at 200+100x,200+50y


wildbill

Ok I understand what you are saying. How do I get the shape to draw in the same position I have drawn it manually? I guess I need to know where the handle is to position it correctly. I know I'm just not seeing something and its driving me nuts.

kevin


   In the first part of the routine the verts are position in screen space.  They're not centered around any point other than 0x,0y (top left corner of screen).     So the handle point in screen space would be 0x,0y.     

   I can't work out if you want to center the shapes vertex around particular vertex, or display it at vertex.  Both are almost the same.

   To handle at vertex, 




; vertex are created in circle, so they located around 0,0
S=NewConVexShape(100,10)

; Handle

setfps 30

Do

Cls

;
Xpos=Mousex()
Ypos=Mousey()

if LeftMouseButton()

Print "Handle at Vertex:"+Str$(CurrentVertex)

; Center to current vertex
if RIghtMouseButton()
if ButtonDown=false
CurrentVertex=Mod(CurrentVertex+1,GetShapeVerts(s,0))
ButtonDown=true
else
ButtonDown=false
endif
endif

Xpos-=GetShapeVertexX(s,CurrentVertex)
Ypos-=GetShapeVertexY(s,CurrentVertex)

endif

DrawShape s,Xpos,Ypos,1

Sync
loop




    or to change the shape so the selected vertex is the center point (0,0 is shapes vertex space)


; vertex are created in circle, so they located around 0,0
S=NewConVexShape(100,10)

CenterAtVertex(s,0)


; Handle
setfps 30

Do

Cls

;
Xpos=Mousex()
Ypos=Mousey()

Print "Handle at Vertex:"+Str$(CurrentVertex)


; Center to current vertex
if RIghtMouseButton()
if ButtonDown=false
CurrentVertex=Mod(CurrentVertex+1,GetShapeVerts(s,0))
CenterAtVertex(s,CurrentVertex)
ButtonDown=true
else
ButtonDown=false
endif
endif

DrawShape s,Xpos,Ypos,1



Sync
loop




Function CenterAtVertex(ThisShape,CenterOnVertex)

CentX#=GetShapeVertexX(ThisShape,CenterOnVertex)
CentY#=GetShapeVertexY(ThisShape,CenterOnVertex)

for lp=0 to GetShapeVerts(ThisShape,0)

vX#=GetShapeVertexX(ThisShape,lp)-CentX#
vY#=GetShapeVertexY(ThisShape,lp)-CentY#
SetShapeVertex ThisShape,lp,vX#,vY#

next
EndFunction





wildbill