Re: Euphoria Chipmunk Physics

new topic     » goto parent     » topic index » view thread      » older message » newer message

Well I added in those changes to the wrapper.

Wrapper

public constant xcpBodyGetPosition = define_c_proc(chip,"cpBodyGetPosition",{C_POINTER,C_POINTER}) 
 
public function cpBodyGetPosition(atom body) 
 
 atom pMem = allocate(16) 
 atom x,y 
  
 c_proc(xcpBodyGetPosition,{pMem,body}) 
 x = float64_to_atom(peek({pMem,8})) 
 y = float64_to_atom(peek({pMem+8,8})) 
 free(pMem) 
  
 return x+y 
	 
end function 
 
--set position of body 
public constant xcpBodySetPosition = define_c_proc(chip,"cpBodySetPosition",{C_POINTER,C_POINTER,C_POINTER,C_POINTER,C_POINTER}) 
 
public procedure cpBodySetPosition(atom body,atom x,atom y) 
 
 atom pMem = allocate(16) 
 sequence xy4 
 poke(pMem,atom_to_float64(x)) 
 poke(pMem+8,atom_to_float64(y)) 
 xy4 = peek4u({pMem,4}) 
 free(pMem) 
 c_proc(xcpBodySetPosition,body&xy4) 
	 
end procedure 
 
public constant xcpBodyGetVelocity = define_c_proc(chip,"cpBodyGetVelocity",{C_POINTER,C_POINTER}) 
 
public function cpBodyGetVelocity(atom body) 
 
 atom pMem = allocate(16) 
 atom x,y 
  
 c_proc(xcpBodyGetVelocity,{pMem,body}) 
 x = float64_to_atom(peek({pMem,8})) 
 y = float64_to_atom(peek({pMem+8,8})) 
 free(pMem) 
 return x+y 
	 
end function 

Example - Demo almost works except for values contain nunVal.

--Demo not currently working 
include std/machine.e 
include std/get.e 
 
include EuChipmunk.ew 
 
atom grav_x = 0 
atom grav_y = -100 
 
sequence gravity = {grav_x,grav_y} 
 
atom space = cpSpaceNew() 
cpSpaceSetGravity(space,gravity[1],gravity[2]) 
 
atom ground = cpSegmentShapeNew(cpSpaceGetStaticBody(space),-20,5,20,-5,0) 
cpShapeSetFriction(ground,1) 
cpSpaceAddShape(space,ground) 
 
atom radius = 5 
atom mass = 1 
 
atom moment = cpMomentForCircle(mass,0,radius,0,0) 
atom ballBody = cpSpaceAddBody(space,cpBodyNew(mass,moment)) 
 
cpBodySetPosition(ballBody,0,10) 
 
atom ballShape = cpSpaceAddShape(space,cpCircleShapeNew(ballBody,radius,0,0)) 
cpShapeSetFriction(ballShape,0.7) 
 
atom timeStep = 1.0 / 60.0 
 
 
for x = 0 to 2 by timeStep do 
	 
atom pos = (cpBodyGetPosition(ballBody)) 
atom vel = cpBodyGetVelocity(ballBody) 
 
 printf(1,"Pos: %d:: Vel: %d",{pos,vel}) 
	 cpSpaceStep(space,timeStep) 
end for 
 
integer key 
while 1 do 
	key = get_key() 
	 
	if key = 27 then 
		exit 
	end if 
end while 
 
 
cpShapeFree(ballShape) 
cpBodyFree(ballBody) 
cpShapeFree(ground) 
cpSpaceFree(space) 
 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu