| 1 | /** | 
|---|
| 2 | * Copyright (c) 2006-2023 LOVE Development Team | 
|---|
| 3 | * | 
|---|
| 4 | * This software is provided 'as-is', without any express or implied | 
|---|
| 5 | * warranty.  In no event will the authors be held liable for any damages | 
|---|
| 6 | * arising from the use of this software. | 
|---|
| 7 | * | 
|---|
| 8 | * Permission is granted to anyone to use this software for any purpose, | 
|---|
| 9 | * including commercial applications, and to alter it and redistribute it | 
|---|
| 10 | * freely, subject to the following restrictions: | 
|---|
| 11 | * | 
|---|
| 12 | * 1. The origin of this software must not be misrepresented; you must not | 
|---|
| 13 | *    claim that you wrote the original software. If you use this software | 
|---|
| 14 | *    in a product, an acknowledgment in the product documentation would be | 
|---|
| 15 | *    appreciated but is not required. | 
|---|
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be | 
|---|
| 17 | *    misrepresented as being the original software. | 
|---|
| 18 | * 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 19 | **/ | 
|---|
| 20 |  | 
|---|
| 21 | #include "wrap_Contact.h" | 
|---|
| 22 | #include "Fixture.h" | 
|---|
| 23 |  | 
|---|
| 24 | namespace love | 
|---|
| 25 | { | 
|---|
| 26 | namespace physics | 
|---|
| 27 | { | 
|---|
| 28 | namespace box2d | 
|---|
| 29 | { | 
|---|
| 30 |  | 
|---|
| 31 | Contact *luax_checkcontact(lua_State *L, int idx) | 
|---|
| 32 | { | 
|---|
| 33 | Contact *c = luax_checktype<Contact>(L, idx); | 
|---|
| 34 | if (!c->isValid()) | 
|---|
| 35 | luaL_error(L, "Attempt to use destroyed contact."); | 
|---|
| 36 | return c; | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | int w_Contact_getPositions(lua_State *L) | 
|---|
| 40 | { | 
|---|
| 41 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 42 | return t->getPositions(L); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | int w_Contact_getNormal(lua_State *L) | 
|---|
| 46 | { | 
|---|
| 47 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 48 | return t->getNormal(L); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | int w_Contact_getFriction(lua_State *L) | 
|---|
| 52 | { | 
|---|
| 53 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 54 | lua_pushnumber(L, t->getFriction()); | 
|---|
| 55 | return 1; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | int w_Contact_getRestitution(lua_State *L) | 
|---|
| 59 | { | 
|---|
| 60 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 61 | lua_pushnumber(L, t->getRestitution()); | 
|---|
| 62 | return 1; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | int w_Contact_isEnabled(lua_State *L) | 
|---|
| 66 | { | 
|---|
| 67 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 68 | lua_pushboolean(L, t->isEnabled()); | 
|---|
| 69 | return 1; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int w_Contact_isTouching(lua_State *L) | 
|---|
| 73 | { | 
|---|
| 74 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 75 | lua_pushboolean(L, t->isTouching()); | 
|---|
| 76 | return 1; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | int w_Contact_setFriction(lua_State *L) | 
|---|
| 80 | { | 
|---|
| 81 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 82 | float f = (float)luaL_checknumber(L, 2); | 
|---|
| 83 | t->setFriction(f); | 
|---|
| 84 | return 0; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | int w_Contact_setRestitution(lua_State *L) | 
|---|
| 88 | { | 
|---|
| 89 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 90 | float r = (float)luaL_checknumber(L, 2); | 
|---|
| 91 | t->setRestitution(r); | 
|---|
| 92 | return 0; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | int w_Contact_setEnabled(lua_State *L) | 
|---|
| 96 | { | 
|---|
| 97 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 98 | bool e = luax_checkboolean(L, 2); | 
|---|
| 99 | t->setEnabled(e); | 
|---|
| 100 | return 0; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | int w_Contact_resetFriction(lua_State *L) | 
|---|
| 104 | { | 
|---|
| 105 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 106 | t->resetFriction(); | 
|---|
| 107 | return 0; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | int w_Contact_resetRestitution(lua_State *L) | 
|---|
| 111 | { | 
|---|
| 112 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 113 | t->resetRestitution(); | 
|---|
| 114 | return 0; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | int w_Contact_setTangentSpeed(lua_State *L) | 
|---|
| 118 | { | 
|---|
| 119 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 120 | float speed = (float) luaL_checknumber(L, 2); | 
|---|
| 121 | t->setTangentSpeed(speed); | 
|---|
| 122 | return 0; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | int w_Contact_getTangentSpeed(lua_State *L) | 
|---|
| 126 | { | 
|---|
| 127 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 128 | lua_pushnumber(L, t->getTangentSpeed()); | 
|---|
| 129 | return 1; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | int w_Contact_getChildren(lua_State *L) | 
|---|
| 133 | { | 
|---|
| 134 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 135 | int a, b; | 
|---|
| 136 | t->getChildren(a, b); | 
|---|
| 137 | lua_pushnumber(L, a + 1); | 
|---|
| 138 | lua_pushnumber(L, b + 1); | 
|---|
| 139 | return 2; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | int w_Contact_getFixtures(lua_State *L) | 
|---|
| 143 | { | 
|---|
| 144 | Contact *t = luax_checkcontact(L, 1); | 
|---|
| 145 | Fixture *a = nullptr; | 
|---|
| 146 | Fixture *b = nullptr; | 
|---|
| 147 | luax_catchexcept(L, [&](){ t->getFixtures(a, b); }); | 
|---|
| 148 |  | 
|---|
| 149 | luax_pushtype(L, a); | 
|---|
| 150 | luax_pushtype(L, b); | 
|---|
| 151 | return 2; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | int w_Contact_isDestroyed(lua_State *L) | 
|---|
| 155 | { | 
|---|
| 156 | Contact *c = luax_checktype<Contact>(L, 1); | 
|---|
| 157 | luax_pushboolean(L, !c->isValid()); | 
|---|
| 158 | return 1; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | static const luaL_Reg w_Contact_functions[] = | 
|---|
| 162 | { | 
|---|
| 163 | { "getPositions", w_Contact_getPositions }, | 
|---|
| 164 | { "getNormal", w_Contact_getNormal }, | 
|---|
| 165 | { "getFriction", w_Contact_getFriction }, | 
|---|
| 166 | { "getRestitution", w_Contact_getRestitution }, | 
|---|
| 167 | { "isEnabled", w_Contact_isEnabled }, | 
|---|
| 168 | { "isTouching", w_Contact_isTouching }, | 
|---|
| 169 | { "setFriction", w_Contact_setFriction }, | 
|---|
| 170 | { "setRestitution", w_Contact_setRestitution }, | 
|---|
| 171 | { "setEnabled", w_Contact_setEnabled }, | 
|---|
| 172 | { "resetFriction", w_Contact_resetFriction }, | 
|---|
| 173 | { "resetRestitution", w_Contact_resetRestitution }, | 
|---|
| 174 | { "setTangentSpeed", w_Contact_setTangentSpeed }, | 
|---|
| 175 | { "getTangentSpeed", w_Contact_getTangentSpeed }, | 
|---|
| 176 | { "getChildren", w_Contact_getChildren }, | 
|---|
| 177 | { "getFixtures", w_Contact_getFixtures }, | 
|---|
| 178 | { "isDestroyed", w_Contact_isDestroyed }, | 
|---|
| 179 | { 0, 0 } | 
|---|
| 180 | }; | 
|---|
| 181 |  | 
|---|
| 182 | extern "C"int luaopen_contact(lua_State *L) | 
|---|
| 183 | { | 
|---|
| 184 | return luax_register_type(L, &Contact::type, w_Contact_functions, nullptr); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | } // box2d | 
|---|
| 188 | } // physics | 
|---|
| 189 | } // love | 
|---|
| 190 |  | 
|---|