Posted: 30th Jan 2021 4:46
I am trying to use the 'SetSpriteShapeChain' function, without success
In the following example the 'spr' variable equals always to 0.0 and no shape is drawn

+ Code Snippet
SetErrorMode(2)

SetWindowTitle( "chain" )
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1)

SetVirtualResolution(1024, 768)
SetOrientationAllowed(1, 1, 1, 1)
SetScissor(0,0,0,0)
UseNewDefaultFonts(1)

type _Point
	x as float
	y as float
endtype

global mypoints as _Point[]
global spr as integer
global sprW as float
global sprH as float

mypoints.length = 6
mypoints[0].x = 0.49 : mypoints[0].y = 0.39
mypoints[1].x = -0.49 : mypoints[1].y = 0.39
mypoints[2].x = -0.31 : mypoints[2].y = -0.02
mypoints[3].x = -0.24 : mypoints[3].y = -0.33
mypoints[4].x = 0.22 : mypoints[4].y = -0.33
mypoints[5].x = 0.29 : mypoints[5].y = -0.02
mypoints[6].x = 0.46 : mypoints[6].y = 0.04

spr = CreateSprite(LoadImage("img1.png"))
sprW = GetSpriteWidth(spr)
sprH = GetSpriteHeight(spr)
SetSpriteAnimation(spr, 512, 408, 17)
PlaySprite(spr)

i as integer
l as integer

l = mypoints.length
for i = 0 to l
	SetSpriteShapeChain(spr, l + 1, i, 1, mypoints[i].x * sprW, mypoints[i].y * sprH, 0)
next

SetPhysicsDebugOn()
SetSpritePhysicsOn(spr, 1)

do
	spr = GetSpriteHitCategory(0xFFFF, ScreenToWorldX(GetRawMouseX()), ScreenToWorldY(GetRawMouseY()))
	Print("Sprite=" + str(spr))
	Sync()
loop
Posted: 31st Jan 2021 21:55
I think you need to start with SetSpriteShapeChain() and then use AddSpriteShapeChain()

Look in the Examples provided under Physics/ChainShape

Actually looking at that example it doesn't use AddSpriteShapeChain() at all. It does work though

[size=large]You are setting the object to be static. I imagine you want it to be dynamic[/size]
Posted: 31st Jan 2021 22:12
at a glance, [s]you keep changing the numPoints parameter when the number shouldn't change[/s].
no, that's not it. looking closer, now.
Posted: 31st Jan 2021 22:21
Guess what? It tunnels
+ Code Snippet
SetErrorMode(2)

SetWindowTitle( "chain" )
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1)

SetVirtualResolution(256, 192)
SetOrientationAllowed(1, 1, 1, 1)
SetScissor(0,0,0,0)
UseNewDefaultFonts(1)

type _Point
	x as float
	y as float
endtype

global mypoints as _Point[]
global spr as integer
global sprW as float
global sprH as float

SetPhysicsDebugOn()
SetPhysicsGravity(0, 5)

mypoints.length = 6
mypoints[0].x = 0.49 : mypoints[0].y = 0.39
mypoints[1].x = -0.49 : mypoints[1].y = 0.39
mypoints[2].x = -0.31 : mypoints[2].y = -0.02
mypoints[3].x = -0.24 : mypoints[3].y = -0.33
mypoints[4].x = 0.22 : mypoints[4].y = -0.33
mypoints[5].x = 0.29 : mypoints[5].y = -0.02
mypoints[6].x = 0.46 : mypoints[6].y = 0.04

spr = CreateSprite(0)
SetSpriteVisible(spr, 0)
sprW = GetSpriteWidth(spr)
sprH = GetSpriteHeight(spr)

i as integer
l as integer

l = mypoints.length
for i = 0 to l
	SetSpriteShapeChain(spr, l + 1, i, 1, mypoints[i].x * sprW, mypoints[i].y * sprH, 0)
next

SetSpritePosition(spr, 50, 50)


SetSpritePhysicsOn(spr, 2)
SetSpritePhysicsAngularVelocity(spr, 1)

do
	//spr = GetSpriteHitCategory(0xFFFF, ScreenToWorldX(GetRawMouseX()), ScreenToWorldY(GetRawMouseY()))
	Print("Sprite=" + str(spr))
	Sync()
loop
Posted: 31st Jan 2021 22:30
and i'd guess its to do with the fact that chains are hollow? probably better to use polygons when trying to "pick" a sprite with mouse coords.
Posted: 31st Jan 2021 22:40
Actually it happens with shape chains AND regular polygons. When the last point comes in contact with another object
Check it out (Comment out the chain loop to see the polygon loop)
+ Code Snippet
SetErrorMode(2)

SetWindowTitle( "chain" )
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1)

SetVirtualResolution(256, 192)
SetOrientationAllowed(1, 1, 1, 1)
SetScissor(0,0,0,0)
UseNewDefaultFonts(1)

type _Point
	x as float
	y as float
endtype

global mypoints as _Point[]
global spr as integer
global sprW as float
global sprH as float

SetPhysicsDebugOn()
SetPhysicsGravity(0, 5)


mypoints.fromJSON('[{"x": 0.49, "y": 0.39}, {"x": -0.49, "y": 0.39}, {"x": -0.31, "y": -0.02}, {"x": -0.24, "y": -0.33}, {"x": 0.22, "y": -0.33}, {"x": 0.29, "y": -0.02}, {"x": 0.46, "y": 0.04}]')


spr = CreateSprite(0)
SetSpriteVisible(spr, 0)
SetSpriteSize(spr, 20, 20)

sprW = GetSpriteWidth(spr)
sprH = GetSpriteHeight(spr)

i as integer
l as integer
a as integer


l = mypoints.length
AddSpriteShapeChain(spr, l + 1, 0, 1, mypoints[0].x * sprW, mypoints[0].y * sprH)
for i = 1 to l
	if i = 0
		a = 0
	else
		a = 1
	endif
	SetSpriteShapeChain(spr, l + 1, i, 1, mypoints[i].x * sprW, mypoints[i].y * sprH, a)
next

//~l = mypoints.length
//~for i = 1 to l
//~	if i = 0
//~		a = 0
//~	else
//~		a = 1
//~	endif
//~	SetSpriteShapePolygon(spr, l + 1, i, mypoints[i].x * sprW, mypoints[i].y * sprH, a)
//~next


SetSpritePosition(spr, 100, 100)


SetSpritePhysicsOn(spr, 2)
SetSpritePhysicsAngularVelocity(spr, 1)

x as float
y as float

do
	
	x = GetWorldXFromSprite(spr, mypoints[l].x * sprW, mypoints[l].y * sprH)
	y = GetWorldYFromSprite(spr, mypoints[l].x * sprW, mypoints[l].y * sprH)	
	DrawEllipse(x, y, 1, 1, 0xFF00ff00, 0xff00ff00, 1)
	//spr = GetSpriteHitCategory(0xFFFF, ScreenToWorldX(GetRawMouseX()), ScreenToWorldY(GetRawMouseY()))
	Print("Sprite=" + str(spr))
	Sync()
loop

Posted: 4th Feb 2021 11:00
Thank you guys for taking the time to look at it.
and i'd guess its to do with the fact that chains are hollow? probably better to use polygons when trying to "pick" a sprite with mouse coords.

I don't think so. Reading from the manual:
... Chains are rigid and can be used to create hollow concave polygons, ...

Correct me if I am wrong, but judging from blink0k's video, we have a bug here.